“无法建立与服务器的稳定连接”

时间:2015-04-08 10:59:26

标签: swift imap mailcore2

我尝试使用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

下一步我创建了获取操作并获得错误

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 =无法建立与服务器的稳定连接。}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

这是工作代码人员:

    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并且它运行正常,只需确保使用正确的端口。