Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:GMail API-通过电子邮件发送附件_Ios_Objective C_Iphone_Gmail Api - Fatal编程技术网

iOS:GMail API-通过电子邮件发送附件

iOS:GMail API-通过电子邮件发送附件,ios,objective-c,iphone,gmail-api,Ios,Objective C,Iphone,Gmail Api,我能够成功地发送没有附件的电子邮件。 然而,当我尝试使用GTLUPLoadParameters上传附件时,我得到了一个501错误 我尝试从照片库中添加附件的NSData,以及发送图像的URL 在这两种情况下,我都犯了同样的错误 // Create the message GTLGmailMessage *message = [[GTLGmailMessage alloc]init]; message.raw = [self getFormattedRawMessageForMail:mail];

我能够成功地发送没有附件的电子邮件。 然而,当我尝试使用GTLUPLoadParameters上传附件时,我得到了一个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];
}
-(NSData*)getFormattedRawMessageForMail:(ORBEmail*)邮件文件名:(NSMutableArray*)arrFilenames{
//日期字符串
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:@“日期:%@\r\n”,标准日期];
//从字符串
NSString*from=@“from:\r\n”;
//串
NSString*to=@“收件人:\r\n”,
//CC字符串
NSString*cc=@;
如果(mail.cc.length>0){
cc=[self-getFormattedStringForFieldName:SEND_MAIL_cc for all receivers:MAIL.cc];
}
//密件抄送字符串
NSString*bcc=@;
如果(mail.bcc.length>0){
密件抄送=[self-getFormattedStringForFieldName:SEND_MAIL_bcc for AllReceivers:MAIL.bcc];
}
//主题字符串
NSString*subject=@“subject:Sample subject\r\n\r\n”;
//身体线
NSString*body=@“示例正文\r\n”;
//要返回的最终字符串
NSString*rawMessage=@;
//根据电子邮件是否有附件,电子邮件可以“文本/普通”或“多部分/混合”发送
如果(arrFilenames.count>0){
//作为“多部分/混合”发送
NSString*contentTypeMain=@“内容类型:多部分/混合;边界=\”项目\“\r\n”;
//可重用边界字符串
NSString*boundary=@“\r\n--项目\r\n”;
//身体线
NSString*contentTypePlain=@“内容类型:text/plain;字符集=\“UTF-8\”\r\n”;
//将字符串从“finalDate”组合到“body”
rawMessage=[contentTypeMain stringByAppendingString:finalDate]stringByAppendingString:from]stringByAppendingString:to]stringByAppendingString:cc]stringByAppendingString:bcc]stringByAppendingString:subject]stringByAppendingString:boundary]stringByAppendingString:contentTypePlain]stringByAppendingString:body];
//附件字符串
for(文件名中的NSString*文件名){
//图像内容类型字符串
NSString*contentTypePNG=边界;
contentTypePNG=[contentTypePNG stringByAppendingString:[NSString stringWithFormat:@“内容类型:图像/png;名称=\”%@\“\r\n”,文件名]];
contentTypePNG=[contentTypePNG stringByAppendingString:@“内容传输编码:base64\r\n”];
//PNG图像数据
NSData*pngData=UIImagePNGRepresentation([Utilities getImageFromFolder:FOLDER_UPLOADS filename:filename]);
NSString*pngString=[NSString stringWithFormat:@“%@\r\n”,GTLEncodeBase64(pngData)];
contentTypePNG=[contentTypePNG stringByAppendingString:pngString];
//添加到原始消息
rawMessage=[rawMessage stringByAppendingString:contentTypePNG];
}
//尾串
rawMessage=[rawMessage stringByAppendingString:@“\r\n--project--”;
}否则{
//以“文本/纯文本”形式发送
rawMessage=[finalDate stringByAppendingString:from]stringByAppendingString:to]stringByAppendingString:cc]stringByAppendingString:bcc]stringByAppendingString:subject]stringByAppendingString:body];
}
返回[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--
内容类型:多部分/混合;boundary=“项目”
日期:2016年7月8日星期五14:25:32+0800
发件人:
致:
主题:样本主题
--计划
内容类型:文本/纯文本;charset=“UTF-8”
样本体
--计划
内容类型:图片/png;name=“SampleImage1.jpg”
内容传输编码:base64
--计划
内容类型:image/png;name=“SampleImage2.jpg”
内容传输编码:base64
--计划--

如何在应用程序中发送消息。使用gmail app ya您的appMy应用程序,它反过来使用gmail REST API。您遇到了501错误,因为请求试图执行未知的方法或操作,或者请求的操作尚未实现。请确保您的Mac上运行了web服务,并且其端口设置为8080。请检查t他的。@abielita此Web服务由Gmail提供,因此理想情况下应该启动!如果邮件正文包含HTML内容怎么办?如何为HTML类型的文本配置“rawMessage”?