【问题标题】:Error when I'm using MailCore2 in Swift 2在 Swift 2 中使用 MailCore2 时出错
【发布时间】:2016-08-27 04:51:45
【问题描述】:

我在我的 iOS 应用程序中使用 Swift 的 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.})

【问题讨论】:

  • 您的凭据似乎有误。
  • 但我确信从我的凭据中我也激活了 gmail 帐户的设置!
  • @AndyIbanez 我应该做一些关于 OAuth2Token 的事情吗?如果是的话,我怎么能在 Swift 中做到这一点?

标签: ios swift mailcore2


【解决方案1】:

我已经从 gmail 帐户更改了不安全应用程序的设置,但我通过从此链接解锁 gmail 帐户解决了凭据问题 https://accounts.google.com/DisplayUnlockCaptcha

【讨论】:

    【解决方案2】:

    就我而言,我不得不“更改不太安全的应用的帐户访问权限”

    https://support.google.com/accounts/answer/6010255?hl=en

    如果有人遇到这个问题,知道这实际上是谷歌方面的安全问题并且必须处理它是非常有帮助的......我会留下它,因为当这发生在我身上时,错误并没有说明什么我必须搜索问题才能发现您必须转到 google 上的那个链接并找出如何正确设置它,我浪费了时间,这对其他想要通过 smtp 使用 google 帐户发送邮件的人会有所帮助

    【讨论】:

      猜你喜欢
      • 2016-07-17
      • 2018-02-22
      • 2016-01-10
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多