使用mailcore发送Microsoft Exchange电子邮件的确切主机名是什么

时间:2014-10-17 06:22:14

标签: mailcore2

我无法使用mailcore发送MS Exchange电子邮件。 它始终返回错误“无法建立与服务器的稳定连接”。 这是我的代码

    smtpSession.hostname = @"smtp.outlook.office365.co.uk";
    smtpSession.username = [currentUser objectForKey:@"email_account"];
    smtpSession.password = [currentUser objectForKey:@"email_password"];
    smtpSession.port = 25;
    smtpSession.connectionType = MCOConnectionTypeClear;

我认为是因为主机名。 在这种情况下,有人能告诉我MS Exchange的主要名称是什么吗?

2 个答案:

答案 0 :(得分:0)

最后,我在这里找到答案

smtpSession.hostname = @"smtp.office365.com";
    smtpSession.username = [currentUser objectForKey:@"email_account"];
    smtpSession.password = [currentUser objectForKey:@"email_password"];
    smtpSession.port = 25;
    smtpSession.connectionType = MCOConnectionTypeStartTLS;

答案 1 :(得分:0)

以下是其他人的参考资料,这是使用CocoaPods添加MailCore2的完整示例

#import <MailCore/MailCore.h>

MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.outlook.office365.com";
smtpSession.username = @"user@domain.com";
smtpSession.password = @"NotMyPass";
smtpSession.port = 587;
smtpSession.connectionType = MCOConnectionTypeStartTLS;

MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
[[builder header] setFrom:[MCOAddress addressWithDisplayName:@"Me" mailbox:@"user@domain.com"]];
[[builder header] setTo:@[[MCOAddress addressWithDisplayName:@"To you" mailbox:@"user@domain.com"]]];
[[builder header] setSubject:@"Mailcore test"];
[builder setTextBody:@"Message received"];
NSData * rfc822Data = [builder data];

MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
    if(error) {
        NSLog(@"Error sending email:%@", error);
    } else {
        NSLog(@"Successfully sent email!");
    }
}];