How send Email using Gmail api in swift

前端 未结 4 1962
一个人的身影
一个人的身影 2021-01-03 08:15

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 source

4条回答
  •  旧巷少年郎
    2021-01-03 09:06

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

提交回复
热议问题