我在Objective-C的iOS9上使用mailcore2设置outlook.com。我已经使用了imap和smtp的gmail工作。我可以从outlook.com上获取电子邮件,但是当我尝试发送电子邮件时,我收到以下错误:
Error Domain = MCOErrorDomain Code = 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
操作,但也犯了同样的错误。
答案 0 :(得分:0)
我无法从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;
答案 1 :(得分: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!")
}
}
}