在Swift中发送Mailcore2普通电子邮件

时间:2015-07-17 22:00:41

标签: 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中成功发送电子邮件?在此先感谢所有回复的人。

4 个答案:

答案 0 :(得分:26)

以下是我的表现:

步骤1)导入mailcore2,我使用cocoapods

text box

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

pod 'mailcore2-ios'

步骤3)转换为swift

#import <MailCore/MailCore.h>

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

答案 1 :(得分:6)

对于Swift 3,只需复制

即可
    let smtpSession = MCOSMTPSession()
    smtpSession.hostname = "smtp.gmail.com"
    smtpSession.username = "sanipcr7@gmail.com"
    smtpSession.password = "xxxxxxx"
    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: String.Encoding.utf8.rawValue){
                NSLog("Connectionlogger: \(string)")
            }
        }
    }

    let builder = MCOMessageBuilder()
    builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "sanipcr7@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.sendOperation(with: rfc822Data!)
    sendOperation?.start { (error) -> Void in
        if (error != nil) {
            NSLog("Error sending email: \(error)")
        } else {
            NSLog("Successfully sent email!")
        }
    }

答案 2 :(得分:3)

要在swift中将图像作为附件发送,请添加:

    var dataImage: NSData?
    dataImage = UIImageJPEGRepresentation(image, 0.6)!
    var attachment = MCOAttachment()
    attachment.mimeType =  "image/jpg"
    attachment.filename = "image.jpg"
    attachment.data = dataImage
    builder.addAttachment(attachment)

答案 3 :(得分:0)

我会在答案下方的注释中添加此内容,但数量太多。

接受的答案中的步骤特别有用,特别是使用指向您的Google帐户的特殊应用密码的链接。

但是@RoolPaap使用的桥接头与我使用的桥接头不同。我使用了#include "Mailcore.h"

我遵循了这个youtube video

#ifndef MyProjectName_Bridging_Header_h
#define MyProjectName_Bridging_Header_h

#include "Mailcore.h"

#endif /* MyProjectName_Bridging_Header_h */