2016-05-02 67 views
1

我在我的iOS應用程序中使用Swift中的MailCore2在沒有打開的內置郵件應用程序的情況下向後臺發送郵件消息,但是當我在我的應用程序上運行時設備(實際設備),我有這樣的問題:當我在Swift 2中使用MailCore2時發生錯誤

我的發送按鈕完整代碼:

@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!") 
     } 
    } 
} 

錯誤:

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.}) 
+0

看起來像您的憑據是錯誤的。 –

+0

但我敢肯定從我的憑據也激活從Gmail帳戶的設置! –

+0

@AndyIbanez我應該做一些關於OAuth2Token的內容嗎?如果是的話我怎麼能在Swift中做到這一點? –

回答

1

已經更改不安全的應用程序的設置從Gmail帳戶,但我解決了cr通過從此鏈接解鎖Gmail帳戶的edentials問題 https://accounts.google.com/DisplayUnlockCaptcha

+0

很高興你找到了解決方案! –

+0

@RoolPaap是:) –

相關問題