Sending Mailcore2 Plain Emails in Swift

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 have successfully imported the MailCore2 API into my swift project, but I see no documentation (Google search, libmailcore.com, github) on how to turn the following working Objective-C code into Swift Code:

MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = @"matt@gmail.com";
smtpSession.password = @"password";
smtpSession.authType = MCOAuthTypeSASLPlain;
smtpSession.connectionType = MCOConnectionTypeTLS;

MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from = [MCOAddress addressWithDisplayName:@"Matt R"
                                              mailbox:@"matt@gmail.com"];
MCOAddress *to = [MCOAddress addressWithDisplayName:nil 
                                            mailbox:@"hoa@gmail.com"];
[[builder header] setFrom:from];
[[builder header] setTo:@[to]];
[[builder header] setSubject:@"My message"];
[builder setHTMLBody:@"This is a test message!"];
NSData * rfc822Data = [builder data];

MCOSMTPSendOperation *sendOperation = 
   [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
    if(error) {
        NSLog(@"Error sending email: %@", error);
    } else {
        NSLog(@"Successfully sent email!");
    }
}];

Does anyone know how to successfully send an email in Swift using this API? Thanks in advance to all who reply.

  • API for Wikipedia in Objective C?
  • Swift TRON Content Type HeaderBuilder Syntax
  • Google Api error “Multiple methods named 'initWithArray:' found”
  • HTTP Request in swift not working
  • Substantial time to push UIViewController in Twitter completion block
  • Rails API: Authenticate users from native mobile apps using username/password or facebook token
  • 3 Solutions Collect From Internet About “Sending Mailcore2 Plain Emails in Swift”

    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)