How send Email using Gmail api in swift

六月ゝ 毕业季﹏ 提交于 2019-11-29 23:11:34

问题


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


回答1:


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))
    }



回答2:


If your goal is to send an email I suggest using the MailCore library for iOS. In the documentation there are examples just in Objective-c but it is compatible with Swift

This is an example of how to send an email with MailCore and Swift:

var smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "matt@gmail.com"
smtpSession.password = "xxxxxxxxxxxxxxxx"
smtpSession.port = 465
smtpSession.authType = MCOAuthType.SASLPlain
smtpSession.connectionType = MCOConnectionType.TLS
smtpSession.connectionLogger = {(connectionID, type, data) in
    if data != nil {
        if let string = NSString(data: data, encoding: NSUTF8StringEncoding){
            NSLog("Connectionlogger: \(string)")
        }
    }
}

var builder = MCOMessageBuilder()
builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "itsrool@gmail.com")]
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
builder.header.subject = "My message"
builder.htmlBody = "Yo Rool, this is a test message!"

let rfc822Data = builder.data()
let sendOperation = smtpSession.sendOperationWithData(rfc822Data)
sendOperation.start { (error) -> Void in
    if (error != nil) {
        NSLog("Error sending email: \(error)")
    } else {
        NSLog("Successfully sent email!")
    }
} 



回答3:


According to Google API documentation(https://developers.google.com/gmail/api/guides/sending), we can know there are have two ways can help you send message success(draft.send, message.send). without a doubt, both of them use api are different, but the result is the same. As you give one of the above way can got that, we just call it message.send. With another way is draft.send, so we should upload "UploadParameters" to Gmail draft, after we upload success, we can get some message about gmail draft. At last, we should send mail content, when we send success, the draft message will automatic delete. Code goes be as follows:

func postEmailMessageRequest(model: MEMailMessageModel, request: CompletionRequest) {

        let uploadParameters = GTLUploadParameters()
        uploadParameters.data = model.snippet.dataUsingEncoding(NSUTF8StringEncoding)
        uploadParameters.MIMEType = model.mimeType!

        let query = GTLQueryGmail.queryForUsersDraftsCreateWithUploadParameters(uploadParameters) as! GTLQueryProtocol!
        service.executeQuery(query) { (ticket : GTLServiceTicket!, messages : AnyObject!, error : NSError?) -> Void in
            if error == nil {
                let messages = (messages as! GTLGmailDraft)
                messages.message.threadId = model.threadID
                self.sendMailRequest(messages, model: model, request: request)
            } else {
                request(status: false, result: "upload success")
            }
        }
    }

    func sendMailRequest(draft: GTLGmailDraft, model: MEMailMessageModel, request: CompletionRequest) {

        let query = GTLQueryGmail.queryForUsersDraftsSendWithUploadParameters(nil) as! GTLQueryGmail
        draft.message.raw = self.generateRawString(model)
        query.draft = draft

        self.service.executeQuery(query, completionHandler: { (ticket, response, error) -> Void in
            if error == nil {
                request(status: true, result: "send success") // after this, the draft will be delete
            } else {
                request(status: false, result: "send failure")
            }
        })
    }

    func generateRawString(model: MEMailMessageModel) -> String {
        let date = MEMailMessagelFormatUtil.coverDateFromDate(NSDate(), dateFormat: "EEE, dd MMM yyyy HH:mm:ss Z")! // get current date

        var fromEamil = ""
        if let str = NSUserDefaults.standardUserDefaults().valueForKey("userEmail") as? String {
            fromEamil = str
        }

        var rawMessage = ""
        if model.isReply { // how to reply, but how to reply the email is another question.
            rawMessage = "" + "In-Reply-To: \(model.messageID!)\r\n"
            if let references = model.references {
                rawMessage += "References: \(references) \(model.messageID!)\r\n"
            } else {
                rawMessage += "References: \(model.messageID!)\r\n"
            }
        }

        rawMessage += "Date: \(date)\r\n" +
            "From: <\(fromEamil)>\r\n" +
            "To: \(model.toName!) <\(model.toMail)>\r\n" +
            "Subject: \(model.subject) \r\n\r\n" + "\(model.snippet)"

        return GTLEncodeWebSafeBase64(rawMessage.dataUsingEncoding(NSUTF8StringEncoding))
    }


来源:https://stackoverflow.com/questions/33259916/how-send-email-using-gmail-api-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!