如何快速使用 Gmail api 发送电子邮件 [英] How send Email using Gmail api in swift

查看:50
本文介绍了如何快速使用 Gmail api 发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gmail Api 没有关于如何执行此操作的明确文档,我已经尝试使用这个,但有很多事情悬而未决.>

我已经寻求外部资源.来源 1来源 2.第一个似乎利用了api的潜力,使用函数queryForUsersMessagesSendWithUploadParameters.

虽然第二个有点多.虽然这在 Objective-C 中不是问题,但 GTMMIMEDocument 对象除外,它不知道从哪里或是否从或库中获取.

我的问题是,是否有人有更清晰和/或更易于理解的代码,或者更好的发送电子邮件指南

解决方案

我找到了解决方案

class func sendEmail() {var gtlMessage = GTLGmailMessage()gtlMessage.raw = self.generateRawString()让 appd = UIApplication.sharedApplication().delegate as!应用程序委托让查询 = GTLQueryGmail.queryForUsersMessagesSendWithUploadParameters(nil)query.message = gtlMessageappd.service.executeQuery(query, completionHandler: { (ticket, response, error) -> Void inprintln("票\(票)")println("响应\(响应)")println("错误\(错误)")})}类 func generateRawString() ->细绳 {var dateFormatter:NSDateFormatter = NSDateFormatter()dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z";//RFC2822-格式var todayString:String = dateFormatter.stringFromDate(NSDate())var rawMessage = "" +"日期:\(todayString)\r\n" +"发件人:<邮件>\r\n" +"收件人:用户名 <邮件>\r\n" +"主题:测试发送电子邮件\r\n\r\n" +测试体"println("消息\(rawMessage)")返回 GTLEncodeWebSafeBase64(rawMessage.dataUsingEncoding(NSUTF8StringEncoding))}

The Gmail Api has no clear documentation on how to do this, I have been trying with this but there are many things that are in the air.

I have sought external sources such. Source 1 and Source 2. The first seems to use the potential of the api, using the function queryForUsersMessagesSendWithUploadParameters.

While the second is about a little more. Although this in Objective-C is not a problem, except for the GTMMIMEDocument object, which do not know where or if it is obtained or a library.

My question is if someone has a somewhat cleaner and / or code easier to understand, or a better guide in which to send an email

解决方案

I found the solution

class func sendEmail() {

        var gtlMessage = GTLGmailMessage()
        gtlMessage.raw = self.generateRawString()

        let appd = UIApplication.sharedApplication().delegate as! AppDelegate
        let query = GTLQueryGmail.queryForUsersMessagesSendWithUploadParameters(nil)
        query.message = gtlMessage

        appd.service.executeQuery(query, completionHandler: { (ticket, response, error) -> Void in
            println("ticket \(ticket)")
            println("response \(response)")
            println("error \(error)")
        })
    }

    class func generateRawString() -> String {

        var dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format
        var todayString:String = dateFormatter.stringFromDate(NSDate())

        var rawMessage = "" +
            "Date: \(todayString)\r\n" +
            "From: <mail>\r\n" +
            "To: username <mail>\r\n" +
            "Subject: Test send email\r\n\r\n" +
            "Test body"

        println("message \(rawMessage)")

        return GTLEncodeWebSafeBase64(rawMessage.dataUsingEncoding(NSUTF8StringEncoding))
    }

这篇关于如何快速使用 Gmail api 发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆