objective-c - 在 Swift 中发送 Mailcore2 普通邮件

标签 objective-c swift api mailcore2 mailcore

我最近将 MailCore2 合并到我的 Objective-C 项目中,并且运行良好。现在,我正在将应用程序中的代码转换为 Swift。我已成功将 MailCore2 API 导入到我的 swift 项目中,但我没有看到有关如何将以下工作 Objective-C 代码转换为 Swift 代码的文档(Google 搜索、libmailcore.com、github):

MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = @"matt@gmail.com";
smtpSession.password = @"password";
smtpSession.authType = MCOAuthTypeSASLPlain;
smtpSession.connectionType = MCOConnectionTypeTLS;

MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from = [MCOAddress addressWithDisplayName:@"Matt R"
                                              mailbox:@"matt@gmail.com"];
MCOAddress *to = [MCOAddress addressWithDisplayName:nil 
                                            mailbox:@"hoa@gmail.com"];
[[builder header] setFrom:from];
[[builder header] setTo:@[to]];
[[builder header] setSubject:@"My message"];
[builder setHTMLBody:@"This is a test message!"];
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!");
    }
}];

有谁知道如何使用此 API 在 Swift 中成功发送电子邮件?提前感谢所有回复的人。

最佳答案

这是我的做法:

Step 1)导入mailcore2,我用的是cocoapods

pod 'mailcore2-ios'

步骤 2) 将 mailcore2 添加到您的桥接 header :Project-Bridging-Header.h

#import <MailCore/MailCore.h>

步骤 3) 转换为 swift

var smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "matt@gmail.com"
smtpSession.password = "xxxxxxxxxxxxxxxx"
smtpSession.port = 465
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: "itsrool@gmail.com")]
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
builder.header.subject = "My message"
builder.htmlBody = "Yo Rool, this is a test message!"

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!")
    }
} 

注意 您的谷歌帐户可能需要一个特殊的应用密码。参见 https://support.google.com/accounts/answer/185833

关于objective-c - 在 Swift 中发送 Mailcore2 普通邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31485359/

相关文章:

ios - 中央调度、操作队列、异步、 View 将和确实消失

objective-c - 扩展 UITableView 的单元格

swift - 如何区分自定义单元格中的两个文本字段?

swift - 连接到 Pusher ChatKit [Swift]

api - 在Z3中调用某些策略后如何使用枚举常量?

ios - 将 subview 置于键盘前

ios - 如何从 Xcode 中的另一个工作区目标访问应用程序类?

ios - 如何使用 PFUser 更改初始 View Controller ?

java - 音频样本混合或改变音量会导致饱和度和白噪声

javascript - YouTube ytplayer