siva kumar
siva kumar

Reputation: 2925

Unable to send mail using smtp server in swift 3.0

I am unable to send mails through this. I am using mailcore in this project and my code look like this

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var smtpSession = MCOSMTPSession()
        smtpSession.hostname = "smtp.gmail.com"
        smtpSession.username = "[email protected]"
        smtpSession.password = "password"
        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)")
                }
            }
        }

        var builder = MCOMessageBuilder()
        builder.header.to = [MCOAddress(displayName: "aaa", mailbox: "[email protected]")]
        builder.header.from = MCOAddress(displayName: "aaa", mailbox: "[email protected]")
        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!")
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

this is the code I am getting error as

2017-02-09 15:55:32.545008 mail[4153:1180194] Connectionlogger: 220 smtp.gmail.com ESMTP f3sm27649397pga.34 - gsmtp 2017-02-09 15:55:32.554899 mail[4153:1180194] Connectionlogger: EHLO iPhone 2017-02-09 15:55:32.792542 mail[4153:1180194] Connectionlogger: 250-smtp.gmail.com at your service, [183.83.32.47]

250-SIZE 35882577

250-8BITMIME

250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH

250-ENHANCEDSTATUSCODES

250-PIPELINING

250-CHUNKING

250 SMTPUTF8 2017-02-09 15:55:32.797741 mail[4153:1180194] Connectionlogger: AUTH PLAIN ODg4Ni5rLnNpdmFAZ21haWwuY29tADg4ODYuay5zaXZhQGdtYWlsLmNvbQA4ODg2MjI4NzY= 2017-02-09 15:55:33.228330 mail[4153:1180194] Connectionlogger: 535-5.7.8 Username and Password not accepted. Learn more at

535 5.7.8 https://support.google.com/mail/?p=BadCredentials f3sm27649397pga.34 - gsmtp 2017-02-09 15:55:33.231723 mail[4153:1180132] Error sending email: Optional(Error Domain=MCOErrorDomain Code=5 "Unable to authenticate with the current session's credentials." UserInfo={NSLocalizedDescription=Unable to authenticate with the current session's credentials.})

it showing error like this,but my credentials are right.

did anyone know solution for this?

Upvotes: 2

Views: 1621

Answers (1)

OJG
OJG

Reputation: 11

google uses OAuth2 for authentication. You have to either implement OAuth2 for mailcore2 - see the wiki at https://github.com/MailCore/mailcore2/wiki/Implementing-OAuth-2.0

or you have to go into your google account sessions and in the security settings allow for 'less secure applications' to connect - which re-enables the username/password authentication over TLS that you are using in the code above.

Upvotes: 1

Related Questions