簡體   English   中英

無法在 swift 3.0 中使用 smtp 服務器發送郵件

[英]Unable to send mail using smtp server in swift 3.0

我無法通過此發送郵件。 我在這個項目中使用 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.
    }


}

這是我收到錯誤的代碼

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 碼 35882577

250-8BITMIME

250-AUTH 登錄 PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH

250 增強狀態代碼

250-流水線

250-塊

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用戶名和密碼不接受。 了解更多信息

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=無法使用當前會話的憑據進行身份驗證。})

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

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

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

或者您必須進入您的谷歌帳戶會話並在安全設置中允許“不太安全的應用程序”連接 - 這將重新啟用您在上面代碼中使用的 TLS 上的用戶名/密碼身份驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM