1

我在 Objective-C 中的 iOS9 上使用 mailcore2 设置 Outlook.com。我的 gmail 已经在使用 imap 和 smtp。我可以从 outlook.com 获取电子邮件,但是当我尝试发送电子邮件时,我收到以下错误:

错误域 = MCOErrorDomain 代码 = 1 “无法建立与服务器的稳定连接。”

以下是我的代码示例:

MCOSMTPSession *session = [[MCOSMTPSession alloc] init];
[session setAuthType:MCOAuthTypeXOAuth2Outlook];
[session setOAuth2Token:accessToken];
[session setUsername:@"user@outlook.com"];
[session setHostname:@"smtp-mail.outlook.com"];
[session setPort:25];  //also tried 587
[session setConnectionType:MCOConnectionTypeStartTLS];  //Also tried MCOConnectionTypeTLS and MCOConnectionTypeClear
[session setCheckCertificateEnabled:false];

MCOSMTPSendOperation *sendOperation = [session sendOperationWithData:data];
[sendOperation start:^(NSError *error) {
}];

我也尝试了该checkAccountOperationWithFrom操作,但这也给出了同样的错误。

4

2 回答 2

1

我无法从 Outlook 获取电子邮件,你能告诉我在我的 Imap 设置中要做哪些更改。下面是我正在使用的代码..

session = [[MCOIMAPSession alloc]init];
session.hostname = @"imap-mail.outlook.com";
session.username = @"dummy@outlook.com";
session.password = nil;
session.port = 993;
session.OAuth2Token = accessToken;
session.connectionType = MCOConnectionTypeStartTLS;
session.authType = MCOAuthTypeSASLLogin;
session.checkCertificateEnabled = false;
于 2019-05-31T09:22:14.330 回答
0

Swift 5、iOS 13、Xcode 版本 11.3.1 (11C504)

Also Need to Disable Captcha :  [https://accounts.google.com/DisplayUnlockCaptcha][1]

回复:邮件发送成功!

这是完美的解决方案:

    @IBAction func btnSendMailClicked(_ sender: UIButton) {

    print(#function)
    let smtpSession = MCOSMTPSession()
    smtpSession.hostname = "smtp.gmail.com"
    smtpSession.username = "emailaddress@gmail.com"
    smtpSession.password = "password" //You can create [App Password from gmail setting][1] 

    smtpSession.port = 587 //25
    smtpSession.authType = MCOAuthType.saslPlain
    smtpSession.connectionType = MCOConnectionType.startTLS
    smtpSession.isCheckCertificateEnabled = false

    smtpSession.connectionLogger = {(connectionID, type, data) in
        if data != nil {
            if let string = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
                NSLog("Connectionlogger: \(string)")
            }
        }
    }

    let builder = MCOMessageBuilder()
    builder.header.to = [MCOAddress(displayName: "Swifty Developers", mailbox: "swiftydevelopers@gmail.com")!]
    builder.header.from = MCOAddress(displayName: "Mehul Parmar", mailbox: "mehulasjack@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: \(String(describing: error))")
        } else {
            NSLog("Successfully sent email!")
        }
    }
}
于 2020-03-05T15:30:30.503 回答