Error when I'm using MailCore2 in Swift 2
Asked Answered
B

2

2

I'm using the MailCore2 at Swift in my iOS application to send message to email in the background without open built-in mail application, But when I'm running app on my device ( real device ) I have this problem:

My complete code for send button:

@IBAction func sendButton(sender: AnyObject) {

    var smtpSession = MCOSMTPSession()
    smtpSession.hostname = "smtp.gmail.com"
    smtpSession.username = "from-email"
    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: NSUTF8StringEncoding){
                NSLog("Connectionlogger: \(string)")
            }
        }
    }

    var builder = MCOMessageBuilder()
    builder.header.to = [MCOAddress(displayName: "AAA", mailbox: "to-email")]
    builder.header.from = MCOAddress(displayName: "RRR", mailbox: "from-email")
    builder.header.subject = messageTitleTextField.text
    var message = messageTextView.text
    builder.htmlBody = 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("Sent successfully, Thanks!")
        }
    }
}

Error:

 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.})
Bernardo answered 2/5, 2016 at 12:13 Comment(3)
Looks like your credentials are wrong.Cinthiacintron
But I'm sure from my credentials also I activate the setting from gmail account !Bernardo
@AndyIbanez Do I should make something about OAuth2Token ? and If yes how I can make that in Swift ?Bernardo
B
2

Already I change setting of unsecure app from gmail account but I solved the credentials problem by unlocking gmail account from this link https://accounts.google.com/DisplayUnlockCaptcha

Bernardo answered 3/5, 2016 at 13:50 Comment(0)
C
0

In my case I had to "Change account access for less secure apps"

https://support.google.com/accounts/answer/6010255?hl=en

If somebody has this problem its quite helpful to know that it is actually problem with security on google side and there it has to be handled...I will leave it because when this happened to me the error didn't say anything I had to search for problem to find out that you have to go to that link on google and find out how to setup it correctly, I wasted time , it will be helpful for others who wants to send mail by smtp with google account

Cocke answered 13/3, 2019 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.