【问题标题】:"A stable connection to the server could not be established"“无法建立与服务器的稳定连接”
【发布时间】:2015-06-13 07:12:30
【问题描述】:

我尝试使用 OAuth2 连接到 Google。我有 access_token 和用户电子邮件。这里没有问题。

let singleton: Singleton = Singleton.sharedInstance 
singleton.session.authType = MCOAuthType.XOAuth2 // session:IMAPSession
singleton.session.OAuth2Token = singleton.accessToken
singleton.session.username = singleton.email
singleton.session.hostname = "imap.gmail.ru"
singleton.session.port = 993
singleton.session.connectionType = MCOConnectionType.StartTLS

下一步我创建 fetch 操作并得到一个错误

let requestKind = MCOIMAPMessagesRequestKind.Headers  | MCOIMAPMessagesRequestKind.Flags | MCOIMAPMessagesRequestKind.Structure
    let uids = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
    let folder = "INBOX"

    let fetchOperation = singleton.session.fetchMessagesOperationWithFolder(folder, requestKind: requestKind, uids: uids)
    fetchOperation.start { (error, fetchedMessages, vanishedMessages) -> Void in
        if (error != nil)
        {
            println("Error: \(error)") // this line return me error
        }

错误:错误域=MCOErrorDomain 代码=1“无法建立与服务器的稳定连接。” UserInfo=0x7f9e260036b0 {NSLocalizedDescription=无法建立到服务器的稳定连接。} 错误:错误域 = MCOErrorDomain 代码 = 1“无法建立与服务器的稳定连接。” UserInfo=0x7f9e23c46c00 {NSLocalizedDescription=无法建立与服务器的稳定连接。}

可能是什么问题?

【问题讨论】:

  • 正确的主机名是imap.gmail.com,而不是imap.gmail.ru
  • @DinhViêtHoà ,我昨天修好了。但我收到新错误:无法使用当前会话的凭据进行身份验证。我尝试更改端口和连接类型,但之后我再次得到:无法建立与服务器的稳定连接
  • “无法验证”可能是正确的错误消息。这意味着您可以连接到 Gmail 服务器。您只需要检查您的 OAuth2 身份验证并在 imap 会话上使用连接记录器来调试您的东西。
  • @DinhViêtHoà 问题可能出在我的 URL 请求中吗? let url = NSURL(string:(NSString(format: "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=%@&redirect_uri=%@&scope=%@&data-requestvisibleactions=%@",client_id,callback,scope,visibleactions))) self.webView.loadRequest(NSURLRequest(URL: url!))
  • 我无法就与 OAuth2 相关的问题提出建议,只能使用 mailcore。

标签: swift imap mailcore2


【解决方案1】:

这是工作代码,伙计们:

    var smtpSession = MCOSMTPSession()
    smtpSession.hostname = "xxxxxxxx"
    smtpSession.username = "xxxxxxx"
    smtpSession.password = "xxxxxxxx"
    smtpSession.port = 8465
    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: "Rool", mailbox: "xxxxxxx@hxxxx.com")]
    builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "xxxxxx@sxxxxxx.com")
    builder.header.subject = "My message"
    builder.htmlBody = "<h3>This is a test message!</h3>"

    let rfc822Data = builder.data()
    let sendOperation = smtpSession.sendOperationWithData(rfc822Data)
    sendOperation.start { (error) -> Void in
        if (error != nil) {
            NSLog("Error sending email: \(error)")
        } else {
            NSLog("Successfully sent email!")
        }
    }

PS:我将此代码用于 SMTP2GO,它运行良好,只要确保您使用的是正确的端口即可。

【讨论】:

    猜你喜欢
    • 2021-01-06
    • 2018-11-28
    • 2017-08-31
    • 2016-12-21
    • 2020-04-06
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多