gpt4 book ai didi

iOS :GMail API - Send Attachments with email

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:03 27 4
gpt4 key购买 nike

我能够成功发送没有附件的电子邮件。但是,当我尝试使用 GTLUploadParamaters 上传附件时,出现 501 错误。

我试过从照片库添加附件的 NSData,以及只发送图像的 URL。

在这两种情况下,我都遇到了同样的错误。

// Create the message
GTLGmailMessage *message = [[GTLGmailMessage alloc]init];
message.raw = [self getFormattedRawMessageForMail:mail];

if(!self.gmailService) {
self.gmailService = [Utilities initializeGmailService];
}
// Get the data of the image present in the photo library
GTLUploadParameters *image = [GTLUploadParameters uploadParametersWithData:[Singleton sharedInstance].dataImage MIMEType:@"image/jpeg"];

GTLQueryGmail *query = [GTLQueryGmail queryForUsersMessagesSendWithUploadParameters:image];
query.userId = [ORBSingleton sharedInstance].userProfile.email;
query.message = message;

[self.gmailService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLGmailMessage *mail, NSError *error) {
if(!error){
handler(YES, error);
}
else{
handler(NO, error);
}
}];

我错过了什么吗?任何帮助将不胜感激。

最佳答案

在添加允许发送的最大大小为 35 mb 的邮件的附件时,我没有使用 GTLGmailMessage。

下面的解决方案中使用了 rfc822 而不是 base64 编码(您当前的解决方案)。

// Create the param
GTLUploadParameters *uploadParam = [[GTLUploadParameters alloc] init];
uploadParam.MIMEType = @"message/rfc822";
uploadParam.data = [self getFormattedRawMessageForMail:mail filenames:arrFilenames];

// Create the query
GTLQueryGmail *query = [GTLQueryGmail queryForUsersMessagesSendWithUploadParameters:uploadParam];
query.userId = @"me";

// Execute query
[self.gmailService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLGmailMessage *mail, NSError *error) {

}];

getFormattedRawMessageForMail:filenames 方法

- (NSData *)getFormattedRawMessageForMail:(ORBEmail *)mail filenames:(NSMutableArray *)arrFilenames{

// 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: <sampleFrom@gmail.com>\r\n”;

// To string
NSString *to = @“To: <sampleTo@gmail.com>\r\n”,

// CC string
NSString *cc = @“”;
if(mail.cc.length > 0) {
cc = [self getFormattedStringForFieldName:SEND_MAIL_CC ForAllReceivers:mail.cc];
}

// BCC string
NSString *bcc = @“”;
if(mail.bcc.length > 0) {
bcc = [self getFormattedStringForFieldName:SEND_MAIL_BCC ForAllReceivers:mail.bcc];
}

// Subject string
NSString *subject = @"Subject: Sample Subject\r\n\r\n”;

// Body string
NSString *body = @“Sample body\r\n”;

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

// Depending on whether the email has attachments, the email can either be sent as "text/plain" or "multipart/mixed"
if (arrFilenames.count > 0) {

// 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];

// Attachments strings
for (NSString *filename in arrFilenames) {

// Image Content Type string
NSString *contentTypePNG = boundary;
contentTypePNG = [contentTypePNG stringByAppendingString:[NSString stringWithFormat:@"Content-Type: image/png; name=\"%@\"\r\n",filename]];
contentTypePNG = [contentTypePNG stringByAppendingString:@"Content-Transfer-Encoding: base64\r\n"];

// PNG image data
NSData *pngData = UIImagePNGRepresentation([Utilities getImageFromFolder:FOLDER_UPLOADS filename:filename]);
NSString *pngString = [NSString stringWithFormat:@"%@\r\n",GTLEncodeBase64(pngData)];
contentTypePNG = [contentTypePNG stringByAppendingString:pngString];

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

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

}else{

// Send as "text/plain"
rawMessage = [[[[[[finalDate stringByAppendingString:from]stringByAppendingString:to]stringByAppendingString:cc]stringByAppendingString:bcc]stringByAppendingString:subject]stringByAppendingString:body];

}

return [rawMessage dataUsingEncoding:NSUTF8StringEncoding];
}

如果邮件有附件,rawMessage 字符串的内容应该是这样的。

Content-Type: multipart/mixed; boundary=“project”
Date: Fri, 08 Jul 2016 14:25:32 +0800
From: <sampleFrom@gmail.com>
To: <sampleTo@gmail.com>
Subject: Sample Subject

--project
Content-Type: text/plain; charset="UTF-8"
Sample body

--project
Content-Type: image/png; name=“SampleImage1.jpg"
Content-Transfer-Encoding: base64

<Image data>

--project
Content-Type: image/png; name=“SampleImage2.jpg"
Content-Transfer-Encoding: base64

<Image data>

--project--

关于iOS :GMail API - Send Attachments with email,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326016/

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