Sending Mailcore2 Plain Emails in Swift

落花浮王杯 提交于 2019-11-28 08:35:02

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 <MailCore/MailCore.h>

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

For Swift 3 just copy this

    let smtpSession = MCOSMTPSession()
    smtpSession.hostname = "smtp.gmail.com"
    smtpSession.username = "sanipcr7@gmail.com"
    smtpSession.password = "xxxxxxx"
    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: String.Encoding.utf8.rawValue){
                NSLog("Connectionlogger: \(string)")
            }
        }
    }

    let builder = MCOMessageBuilder()
    builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "sanipcr7@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.sendOperation(with: rfc822Data!)
    sendOperation?.start { (error) -> Void in
        if (error != nil) {
            NSLog("Error sending email: \(error)")
        } else {
            NSLog("Successfully sent email!")
        }
    }

To send an image as attachment in swift just add:

    var dataImage: NSData?
    dataImage = UIImageJPEGRepresentation(image, 0.6)!
    var attachment = MCOAttachment()
    attachment.mimeType =  "image/jpg"
    attachment.filename = "image.jpg"
    attachment.data = dataImage
    builder.addAttachment(attachment)

I would've added this in the comments below the answer but there are so many.

The steps in the accepted answer worked especially using the link to the special app password for your google account.

But @RoolPaap used a different bridging header then the one I did. I used #include "Mailcore.h"

I followed this youtube video

#ifndef MyProjectName_Bridging_Header_h
#define MyProjectName_Bridging_Header_h

#include "Mailcore.h"

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