我能够成功发送没有附件的电子邮件。 但是,当我尝试使用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);
}
}];
我错过了什么吗?任何帮助将不胜感激。
答案 0 :(得分:7)
添加允许以最大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--