简体   繁体   English

无法在 swift 3.0 中使用 smtp 服务器发送邮件

[英]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我在这个项目中使用 mailcore,我的代码看起来像这样

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 = "user@gmail.com"
        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: "user2@gmail.com")]
        builder.header.from = MCOAddress(displayName: "aaa", mailbox: "user@gmail.com")
        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] 2017年2月9日15:55:32.545008邮件[4153:1180194] Connectionlogger:220 smtp.gmail.com ESMTP f3sm27649397pga.34 - gsmtp 2017年2月9日15:55:32.554899邮件[4153:1180194] Connectionlogger:EHLO iPhone 2017-02-09 15:55:32.792542 邮件[4153:1180194] 连接记录器:250-smtp.gmail.com 为您服务,[183.83.32.47]

250-SIZE 35882577 250 码 35882577

250-8BITMIME 250-8BITMIME

250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-AUTH 登录 PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH

250-ENHANCEDSTATUSCODES 250 增强状态代码

250-PIPELINING 250-流水线

250-CHUNKING 250-块

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. 250 SMTPUTF8 2017年2月9日15:55:32.797741邮件[4153:1180194] Connectionlogger:AUTH PLAIN ODg4Ni5rLnNpdmFAZ21haWwuY29tADg4ODYuay5zaXZhQGdtYWlsLmNvbQA4ODg2MjI4NzY = 2017年2月9日15:55:33.228330邮件[4153:1180194] Connectionlogger:535-5.7.8用户名和密码不接受。 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.}) 535 5.7.8 https://support.google.com/mail/?p=BadCredentials f3sm27649397pga.34 - gsmtp 2017-02-09 15:55:33.231723 mail[4153:1180132] 错误发送电子邮件域= MCOErrorDomain Code=5 "无法使用当前会话的凭据进行身份验证。" UserInfo={NSLocalizedDescription=无法使用当前会话的凭据进行身份验证。})

it showing error like this,but my credentials are right.它显示这样的错误,但我的凭据是正确的。

did anyone know solution for this?有没有人知道这个解决方案?

google uses OAuth2 for authentication.谷歌使用 OAuth2 进行身份验证。 You have to either implement OAuth2 for mailcore2 - see the wiki at https://github.com/MailCore/mailcore2/wiki/Implementing-OAuth-2.0您必须为 mailcore2 实施 OAuth2 - 请参阅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.或者您必须进入您的谷歌帐户会话并在安全设置中允许“不太安全的应用程序”连接 - 这将重新启用您在上面代码中使用的 TLS 上的用户名/密码身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM