2017-02-09 141 views
1

我無法通過此發送郵件。我使用的這個項目mailcore和我的代碼看起來像這樣無法在swift 3.0中使用smtp服務器發送郵件

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. 
    } 


} 

這是我得到的錯誤代碼爲

2017年2月9日15:55:32.545008信箱[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爲您服務,[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年2月9日15:55:33.228330郵件[4153:1180194] Connectionlogger: 535-5.7.8用戶名和密碼不被接受。瞭解更多

535 5.7.8 https://support.google.com/mail/?p=BadCredentials f3sm27649397pga.34 - gsmtp 2017年2月9日15:55:33.231723 信箱[4153:1180132]錯誤發送電子郵件:可選(錯誤 域= MCOErrorDomain代碼= 5「無法與當前 會議的證書進行驗證。」的UserInfo = {NSLocalizedDescription =無法與當前會話的憑據 驗證。})

它顯示這樣的錯誤,但我的憑據是正確的。

有沒有人知道這個解決方案?

回答

0

谷歌使用OAuth2進行身份驗證。您必須爲mailcore2實施OAuth2 - 請參閱維基https://github.com/MailCore/mailcore2/wiki/Implementing-OAuth-2.0

或者您必須進入您的Google帳戶會話,並在安全設置中允許「安全性較低的應用程序」進行連接 - 這將重新啓用用戶名/通過您在上面的代碼中使用的TLS進行密碼驗證。

相關問題