Sending Mailcore2 Plain Emails in Swift

前端 未结 4 1266
醉话见心
醉话见心 2020-12-14 11:38

I\'ve recently incorporated MailCore2 into my Objective-C project, and it has worked perfectly. Now, I am in the process of transitioning the code within the app to Swift. I

4条回答
  •  星月不相逢
    2020-12-14 11:57

    Here's how I did it:

    Step 1) Import mailcore2, I'm using cocoapods

    pod 'mailcore2-ios'
    

    Step 2) Add mailcore2 to your bridging header: Project-Bridging-Header.h

    #import 
    

    Step 3) Translate to 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!")
        }
    } 
    

    Note You might be needing a special app password for your google account. See https://support.google.com/accounts/answer/185833

提交回复
热议问题