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

2024-05-30

我能够成功发送没有附件的电子邮件。 但是,当我尝试使用 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。

代替base64编码(您当前的解决方案),rfc822用于下面的解决方案中。

// 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) {

}];

The getFormattedRawMessageForMail:文件名 method

- (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: <[email protected] /cdn-cgi/l/email-protection>\r\n”;

    // To string
    NSString *to = @“To: <[email protected] /cdn-cgi/l/email-protection>\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: <[email protected] /cdn-cgi/l/email-protection>
To: <[email protected] /cdn-cgi/l/email-protection>
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--
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS:GMail API - 通过电子邮件发送附件 的相关文章

  • 当点击 UITableViewCell 的子视图时引发选择事件 (didSelectRowAtIndexPath)

    我创建了一个自定义 UITableViewCell 其中包含许多子视图 在大多数情况下 我希望 UITableViewCell 的控制器来处理事件 在一种情况下 我希望子视图简单地将事件传递给父 UITableViewCell 这将导致它在
  • 即席分发失败

    我在一家大公司工作 正在开发一个适用于 iOS 5 的 iOS 应用程序 分发应用程序的唯一方式是通过临时部署 我拥有自己的服务器已经有一段时间了 由 o2switch 法国托管商 托管 当我开始开发时 我们使用它来部署应用程序以进行 Be
  • 如何在 iPhone 应用程序的 url 中传递字符串值

    NSURLRequest request NSURLRequest requestWithURL NSURL URLWithString http www krsconnect no community api html method ba
  • just_audio 无法在 ios flutter 上工作未处理的异常:(-11800)操作无法完成

    我正在尝试从它自己的存储库运行 just audio 示例项目https github com ryanheise just audio tree master just audio example https github com rya
  • NSMutableData 删除字节?

    我可以使用以下命令轻松地将字节添加到 NSMutableData 实例appendData方法 但是我没有看到任何类似的删除数据的方法 我是否忽略了某些内容 或者我是否需要创建一个新对象并仅复制我需要的字节 请参阅以下方法的文档 void
  • 让约束在尺寸类别中发挥作用

    所以 我正在 Xcode 6 beta 中尝试尺寸类 我对图像设置了一些限制 使其根据 iPhone 纵向和横向对应的尺寸类别处于不同的位置 这些限制在下图中可见 正如您所看到的 当我处于紧凑 紧凑状态时 一些约束被 安装 而其他约束则没有
  • 当 iPhone 设备方向朝上/朝下时,我可以判断它是横向还是纵向吗?

    我得到这个代码 如果设备处于左 右横向或上下颠倒状态 它会旋转并显示另一个视图控制器 但如果它的方向朝上或朝下 那么我如何判断它是横向模式还是纵向模式 因为我只想在它面朝上或朝下以及横向模式下旋转 void viewDidAppear BO
  • UIAlertView 中的 MPVolumeView?

    是否可以将 MPVolumeView 放入 UIAlertView 中 我已经尝试过put它在里面 但不显示 这可能是sizeToFit or initWithFrame 部分 有没有办法测试是否MPVolumeView实际上正在被创建吗
  • 取消交互式 UINavigationController 弹出手势不会调用 UINavigationControllerDelegate 方法

    如果拖动 a 的边缘UIViewController开始交互式流行过渡UINavigationController the UIViewController在电流下方有viewWillAppear 调用 然后是UINavigationCon
  • 处理 NSPropertyListSerialization 中的 CFNull 对象

    在我的应用程序中 我尝试序列化服务器响应字典并将其写入文件系统 但对于某些响应 我收到错误 属性列表格式无效 原因是服务器响应中的 CFNull 对象 现在 服务器响应将不断变化 因此我没有明确的方法来删除 CFNull 对象 下面是我的代
  • 无法下载应用程序 - 此时无法下载“APP”

    我的应用程序有 PLUS 版本和常规版本 我使用不同的目标对它们进行存档 我将 ipa 上传到 TestFlight 也上传到我的曲棍球服务器 PLUS 版本总是下载得很好 但普通版本总是给我 无法下载应用程序 错误 我根本没有更改两个版本
  • 如何将 #ifdef DEBUG 添加到 Xcode?

    我的项目中有一些代码永远不应该在发布版本中使用 但在测试时很有用 我想做这样的事情 ifdef DEBUG Run my debugging only code endif 在 Xcode 4 中哪里添加 DEBUG 设置 我尝试将其放入
  • 使用导航控制器在 Storyboard 中呈现视图控制器 - Swift

    我目前在下面的新故事板中显示了一个 viewController var storyboard UIStoryboard UIStoryboard name AccountStoryboard bundle nil var vc Welco
  • 如何获取 UITableView 内容视图的大小?

    我想在填充表格时获取 UITableView 内容视图的大小 关于如何执行此操作有什么建议吗 Allows you to perform layout before the drawing cycle happens layoutIfNee
  • SDWebImage 显示缓存中图像的占位符

    在 iOS 5 1 项目 iPad 中使用 SDWebImage 3 我们展示相当大的图像 700x500 并且我们有很多图像 1000 我们预取图像并缓存到磁盘 然后允许用户浏览它们 效果很好 除了当您浏览图像时 您总是会看到占位符显示一
  • 带约束的 Swift 动画

    是否可以通过改变约束来制作 UIView 动画 基本上 我想要动画myv UIView 具有 x y 高度和宽度约束 使用 UIView animateWithDuration 1 5 通过改变旧的限制 是的 这是可能的 你可以这样做 fu
  • iOS:addConstraints:应用程序崩溃

    Problem 我似乎无法在现有项目中采用自动布局 Details 我之前也遇到过与此问题相同的问题presentViewController 在 iOS 但所提供的答案都不是我的解决方案 我正在使用所有没有 xib 的故事板视图 我的 使
  • 在应用程序内启用或禁用 Iphone 推送通知

    我有一个 iPhone 应用程序 可以接收推送通知 目前 我可以通过转到 iPhone 设置 通知来禁用我的应用程序的推送通知 但我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知 这是可以做到的 因为我在 foursquare i
  • 如何在 XCode5 中将部署目标更改为 5.1.1 [重复]

    这个问题在这里已经有答案了 我正在一个项目中工作 我需要支持 iOS 5 1 1 但在 部署目标 的下拉菜单中我没有 5 1 1 作为选项 我的问题是如何将 iOS 5 1 1 添加为部署目标 我将非常感谢你的帮助 如果您愿意 您可以在框中
  • 桌面上的 AVAudioSession?

    在 mac 桌面上 我试图录制系统声音 以及可选的麦克风声音 但一开始我只是录制系统声音 我正在遵循本指南 https www appcoda com ios avfoundation framework tutorial https ww

随机推荐

  • 在 python 中创建默认列表

    我正在尝试创建一个非常有用的等效列表collections defaultdict http docs python org library collections html collections defaultdict 下面的设计效果很
  • VB.net 应用程序保留以前的版本

    我有一个正在发布的 Visual Basic 项目 并且每次都会增加版本号 当我安装新版本时 它会打开 但一旦应用程序重新启动 它似乎就会恢复到以前的版本 我不知道为什么 尝试更新发布应用程序时所需的最低版本 转到应用程序属性 gt 发布
  • Meteor的订阅和同步很慢

    我有一个包含 6000 只股票的 10M 文档的集合 股票名称已索引 当我订阅一只新股票时 meteor挂了10多秒 就得到了这只股票的大约3000份文件 同样 在认购了几只股票后 meteor 挂起 CPU 使用率达到 100 Meteo
  • 在没有连接的情况下将 sqlalchemy 核心语句转换为原始 SQL?

    我有一个使用 sqlalchemy 核心的脚本 但不幸的是我需要重写它以使用原始 sql 是否可以在没有显式引擎的情况下将我的 sqla insert etc 语句翻译成特定的方言 这里是 oracle 本质上 能够对 str some s
  • if 子句中的多个条件

    如果我有一个 if 语句需要满足这些要求 if cave gt 0 training gt 0 mobility gt 0 sleep gt 0 有没有办法说它们都大于零 只是为了更高效的 DRY 代码 就像是 if cave traini
  • Swift 无法从上到下呈现视图控制器

    在我的应用程序中 我必须从上到下呈现屏幕 我尝试了下面的代码 它给出了相同的正常呈现风格 let screen self storyboard instantiateViewController withIdentifier Screen1
  • 如何更改 Xamarin.Forms(便携式)应用程序中的 PCL 配置文件

    我只是想知道如何使用 Visual Studio 2015 Update 2 更改 Xamarin 中的 PCL 配置文件 在安装某些软件包时 我收到一条错误消息 该软件包与 PCL 配置文件 259 不兼容 先感谢您 右键单击 PCL 项
  • java中将函数作为参数传递

    我正在熟悉 Android 框架和 Java 并希望创建一个通用的 NetworkHelper 类 该类将处理大部分网络代码 使我能够从中调用网页 我按照developer android com 上的这篇文章创建了我的网络类 http d
  • 如何检测 KTable 连接的哪一侧触发了更新?

    当您在 Kafka 中连接两个表时 每次更新两个 KTable 之一时 您的输出 Ktable 也会更新 想象一下你正在加入Customers与一个列表Orders你已经适当减少了 再次想象一下 您使用此连接的结果来为最终客户提供特别优惠和
  • 滚动高图图表

    这是我的问题 我正在使用phonegap框架来开发一个混合应用程序 并且我需要这个应用程序具有我决定使用highcharts库的图表 问题是 我似乎无法在触摸图表后滚动 至少在触摸图像的选定部分内部时 我想要做的是防止图表发生任何事件 并显
  • 在构造函数中运行代码的不好做法可能会失败?

    我的问题更像是一个设计问题 在 Python 中 如果 构造函数 中的代码失败 则该对象最终不会被定义 因此 someInstance MyClass test123 lets say that constructor throws an
  • Dagger 2 不会注入我的对象,但可以从组件中获取

    我有我的组件 GithubListActivityScope Component modules GithubListActivityModule class GlideActivityModule class public interfa
  • 如何在声明性 Jenkins 管道的阶段之间传递变量?

    如何在声明性管道的阶段之间传递变量 在脚本化管道中 我收集的过程是写入临时文件 然后将该文件读入变量 如何在声明式管道中执行此操作 例如 我想根据 shell 操作创建的变量触发不同作业的构建 stage stage 1 steps sh
  • Swift 客户端和根 SSL 证书身份验证

    积分得到token api of QLIK server with ssl证书 但执行任务后我收到这样的错误 完成时出现错误 代码 999 Your hostname and endpoint let hostname YOUR HOST
  • 如何使用 Jade 迭代数组创建 html 表

    我从 Node ExpressJS 框架开始 遇到了这个我无法解决的问题 我正在尝试显示一个包含一些博客文章的表格 是的 一个博客 但我没有完成 这是 Jade 模板代码 div table thead tr th Posts tbody
  • MySQL如何根据字段是否存在来插入新记录或更新字段?

    我正在尝试实现一个评级系统 在数据库表中保留以下两个字段 评级 当前评级 num rates 迄今为止提交的评分数量 UPDATE mytable SET rating rating num rates theRating num rate
  • 环回关系不填充对象 ID 数组

    到目前为止我有 2 个模型 工作流程核心 工作流程步骤 工作流核心有一个steps属性 该属性是数组类型并且包含1 多个步骤 当呼叫接通时工作流程核心响应正文不会使用实际步骤对象填充步骤数组 工作流程核心 json name workflo
  • Maven Antrun 不执行任务

    我正在使用 Maven AntRun 插件 1 6 从他们的示例中我无法编写要执行的以下 ant 任务的代码 示例网址 http maven apache org plugins maven antrun plugin examples c
  • 使用相同的代码和 git 存储库部署 2 个不同的 heroku 应用程序

    我正在尝试创建 2 个不同的 Heroku 应用程序使用相同的代码使用相同的 git 存储库 App1 是我的朋友在 Heroku 中创建的 我不是合作者 app2 是我尝试部署的同一个 git 存储库的分支 这可能吗 当我尝试将第二个应用
  • iOS:GMail API - 通过电子邮件发送附件

    我能够成功发送没有附件的电子邮件 但是 当我尝试使用 GTLUploadParamaters 上传附件时 出现 501 错误 我尝试添加照片库中附件的 NSData 以及仅发送图像的 URL 在这两种情况下我都得到了同样的错误 Create