gpt4 book ai didi

ios - 如何发送带有附件的GTLRGmail_Message

转载 作者:行者123 更新时间:2023-12-01 18:41:52 32 4
gpt4 key购买 nike

经过一段时间的搜索后,我设法发送了一封电子邮件,但没有附件,如下所示:

NSString *messageString =                              @"From: \"From Email USer Name\" <fromEmail>\r\n";
messageString = [messageString stringByAppendingString:@"To: \"To Email User Name\" <ToEmail>\r\n"];
messageString = [messageString stringByAppendingString:@"Subject: New stuff\r\n"];
messageString = [messageString stringByAppendingString:@"Content-type: text/html;charset=iso-8859-1\r\n\r\n"];
messageString = [messageString stringByAppendingString:@"Hello my friend, \n can you please call me when have free time. \nMark."];

NSData *data = [messageString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64Encoded = GTLREncodeWebSafeBase64(data);


GTLRGmail_Message *message = [[GTLRGmail_Message alloc] init];

message.raw = base64Encoded;

GTLRGmailQuery_UsersMessagesSend *query = [GTLRGmailQuery_UsersMessagesSend queryWithObject:message userId:@"myEmailAddress" uploadParameters:nil];

[self.service executeQuery:query completionHandler:^(GTLRServiceTicket * _Nonnull callbackTicket, id _Nullable object, NSError * _Nullable callbackError) {
NSLog(@"%@",callbackError);
}];

我试图添加 GTLRUploadParameters,如下所示:
GTLRUploadParameters *params = [GTLRUploadParameters uploadParametersWithData:imageData MIMEType:@"message/rfc822"];

但仍然没有成功,我试图将消息拆分为多个 GTLRGmail_MessagePartBody并将它们作为 GTLRGmail_MessagePartBody像这样添加到容器 message.payload中:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"IMG_1253" ofType:@"jpg"];

/***************************** create a imageData part ******************/

NSData *imagedata = [NSData dataWithContentsOfFile:filePath];
NSString *imageBase64String = GTLREncodeWebSafeBase64(imagedata);
GTLRGmail_MessagePartBody *bodyImage = [[GTLRGmail_MessagePartBody alloc] init];
bodyImage.data = imageBase64String;
bodyImage.size = @(imagedata.length);

GTLRGmail_MessagePart *imagePart = [[GTLRGmail_MessagePart alloc] init];
imagePart.body = bodyImage;
imagePart.mimeType = @"image/jpeg";
imagePart.filename = @"IMG_1253";


/***************************** create a message part ******************/

NSString *messageString = @"Hello my friend, \n can you please call me when have free time. \nMark.";
NSData *dataMessage = [messageString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncodedMessage = GTLREncodeWebSafeBase64(dataMessage);

GTLRGmail_MessagePartBody *bodyText = [[GTLRGmail_MessagePartBody alloc] init];
bodyText.data = base64EncodedMessage;
bodyText.size = @(dataMessage.length);


GTLRGmail_MessagePart *messagePart = [[GTLRGmail_MessagePart alloc] init];
messagePart.body = bodyText;
messagePart.mimeType = @"text/plain";

/***************************** create a payload part ******************/

GTLRGmail_MessagePartHeader *toHeader = [[GTLRGmail_MessagePartHeader alloc] init];
toHeader.name = @"to";
toHeader.value = @"userEmail";

GTLRGmail_MessagePart *payload = [[GTLRGmail_MessagePart alloc] init];
payload.mimeType = @"multipart/mixed; boundary=\"IMG_1253\"";
payload.parts = @[messagePart, imagePart];


GTLRGmail_Message *message = [[GTLRGmail_Message alloc] init];
message.payload = payload;

GTLRGmailQuery_UsersMessagesSend *query = [GTLRGmailQuery_UsersMessagesSend queryWithObject:message userId:@"myEmail" uploadParameters:nil];

[self.service executeQuery:query completionHandler:^(GTLRServiceTicket * _Nonnull callbackTicket, id _Nullable object, NSError * _Nullable callbackError) {

NSData *data = callbackError.userInfo[@"data"];
NSString *string = [[NSString alloc] initWithData:data encoding:0];

NSLog(@"%@",string);
}];

但是在这种情况下,我得到: Code=400 "'raw' RFC822 payload message string or uploading message via /upload/* URL required",如果我从以前的解决方案中将 base64Encoded添加到 message.raw中,我会收到电子邮件,但没有附件
我做错了什么?

最佳答案

所以解决方案是稍加修改的Vea answer不是漂亮的,而是:

GTLRUploadParameters *uploadParam = [[GTLRUploadParameters alloc] init];
uploadParam.MIMEType = @"message/rfc822";
uploadParam.data = [self getFormattedRawMessage];

GTLRGmail_Message *message = [[GTLRGmail_Message alloc] init];

GTLRGmailQuery_UsersMessagesSend *query = [GTLRGmailQuery_UsersMessagesSend queryWithObject:message userId:@"constantin.saulenco@gmail.com" uploadParameters:uploadParam];

[self.service executeQuery:query completionHandler:^(GTLRServiceTicket * _Nonnull callbackTicket, id _Nullable object, NSError * _Nullable callbackError) {

NSData *data = callbackError.userInfo[@"data"];
NSString *string = [[NSString alloc] initWithData:data encoding:0];

NSLog(@"%@",string);
}];

getFormattedRawMessage:
- (NSData *)getFormattedRawMessage
{
// Date string
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss Z";
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSString *finalDate = [NSString stringWithFormat:@"Date: %@\r\n", strDate];

// From string
NSString *from = @"From: <constantin.saulenco@gmail.com>\r\n";

// To string
NSString *to = @"To: <constantin.saulenco@mobiversal.com>\r\n";

// CC string
NSString *cc = @"";

// BCC string
NSString *bcc = @"";

// Subject string
NSString *subject = @"Subject: New stuff\r\n\r\n";

// Body string
NSString *body = @"Hello my friend, \n can you please call me when have free time. \nMark. \r\n";

// Final string to be returned
NSString *rawMessage = @"";

// Send as "multipart/mixed"
NSString *contentTypeMain = @"Content-Type: multipart/mixed; boundary=\"project\"\r\n";

// Reusable Boundary string
NSString *boundary = @"\r\n--project\r\n";

// Body string
NSString *contentTypePlain = @"Content-Type: text/plain; charset=\"UTF-8\"\r\n";

// Combine strings from "finalDate" to "body"
rawMessage = [[[[[[[[[contentTypeMain stringByAppendingString:finalDate] stringByAppendingString:from]stringByAppendingString:to]stringByAppendingString:cc]stringByAppendingString:bcc]stringByAppendingString:subject]stringByAppendingString:boundary]stringByAppendingString:contentTypePlain]stringByAppendingString:body];


// Image Content Type string
NSString *contentTypeJPG = boundary;
contentTypeJPG = [contentTypeJPG stringByAppendingString:[NSString stringWithFormat:@"Content-Type: image/jpeg; name=\"%@\"\r\n",@"IMG_1253.jpg"]];
contentTypeJPG = [contentTypeJPG stringByAppendingString:@"Content-Transfer-Encoding: base64\r\n"];

// PNG image data
UIImage *image = [UIImage imageNamed:@"IMG_1253.jpg"];
NSData *jpegData = UIImageJPEGRepresentation(image, 1);
NSString *imageBase64String = GTLREncodeBase64(jpegData);
NSString *pngString = [NSString stringWithFormat:@"%@\r\n",imageBase64String];
contentTypeJPG = [contentTypeJPG stringByAppendingString:pngString];

// Add to raw message
rawMessage = [rawMessage stringByAppendingString:contentTypeJPG];


// End string
rawMessage = [rawMessage stringByAppendingString:@"\r\n--project--"];

return [rawMessage dataUsingEncoding:NSUTF8StringEncoding];
}

但是我仍然知道如何使用 GTLRGmail_MessagePartBodyGTLRGmail_MessagePartHeader作为 message.payload

关于ios - 如何发送带有附件的GTLRGmail_Message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714135/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com