本文介绍了发送Mailcore2普通电子邮件斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近成立MailCore2到我的Objective-C项目,它完美地工作。现在,我在应用中雨燕的过渡code的过程。我已经成功导入了MailCore2 API到我迅速的项目,但我看到如何把下列工作的Objective-C code到斯威夫特code没有文件(谷歌搜索,libmailcore.com,github上):

I've recently incorporated MailCore2 into my Objective-C project, and it has worked perfectly. Now, I am in the process of transitioning the code within the app to Swift. I have successfully imported the MailCore2 API into my swift project, but I see no documentation (Google search, libmailcore.com, github) on how to turn the following working Objective-C code into Swift Code:

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成功发送电子邮件的斯威夫特?在此先感谢所有谁答复。

Does anyone know how to successfully send an email in Swift using this API? Thanks in advance to all who reply.

推荐答案

下面是我是如何做到的:

Here's how I did it:

步骤1)导入mailcore2,我使用的CocoaPods

Step 1) Import mailcore2, I'm using cocoapods

pod 'mailcore2-ios'

第2步)添加mailcore2到你的桥接报头:项目桥接-Header.h

Step 2) Add mailcore2 to your bridging header: Project-Bridging-Header.h

#import <MailCore/MailCore.h>

第3步)翻译成迅速

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

这篇关于发送Mailcore2普通电子邮件斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:05