2017-02-09 136 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进行密码验证。

相关问题