MailCore2使用简介iOS版

来源:互联网 发布:c语言中如何调用函数 编辑:程序博客网 时间:2024/06/08 15:04

OS发送邮件,似乎MFMailComposeViewController(MessageUI)简单方便,但如果你想实现应用内直接发送而绕过系统邮件,两种选择一自己实现,二使用第三方库。

MailCore库继第一个版本MailCore后发布了第二个版本MaiCore2,如果你觉得工程繁杂,可以用pod下载管理(以下demo在xcode6.4 OS10.10.5下载):

$ pod search mailcore2-> MailCore2 (0.4.7) [DEPRECATED in favor of mailcore2-ios]   Mailcore 2   pod 'MailCore2', '~> 0.4.7'   - Homepage: http://libmailcore.com   - Source:     http://d.etpan.org/mailcore2-deps/mailcore2-ios/mailcore2-ios-5.zip   - Versions: 0.4.7, 0.4.6, 0.4.5, 0.3.pre1, 0.2.pre1 [master repo]-> mailcore2-ios (0.5.1)   Mailcore 2 for iOS   pod 'mailcore2-ios', '~> 0.5.1'   - Homepage: http://libmailcore.com   - Source:     http://d.etpan.org/mailcore2-deps/mailcore2-ios/mailcore2-ios-8.zip   - Versions: 0.5.1, 0.5.0, 0.4.7 [master repo]-> mailcore2-osx (0.5.2)   Mailcore 2 for OS X   pod 'mailcore2-osx', '~> 0.5.2'   - Homepage: http://libmailcore.com   - Source:     http://d.etpan.org/mailcore2-deps/mailcore2-osx/mailcore2-osx-7.zip   - Versions: 0.5.2, 0.5.1, 0.5.0, 0.4.7 [master repo]

一、工程引入

下载包,解压后拷贝到你的工程文件里,然后按图显示:


将mailcore2.xcodeproj拖入你的工程里,此时会发现


二、类库引用

添加other linker flags 


Framework添加


三、API使用

写个方法测试一下

#import <MailCore/MailCore.h>

+(void)sendEmail:(NSString*)hosName            port:(unsigned int)port        userName:(NSString*)userName        password:(NSString*)password fromDisplayName:(NSString*)fromDisplayName      fromMaiBox:(NSString*)fromMaiBox         toArray:(NSArray*)toArray         ccArray:(NSArray*)ccArray        bccArray:(NSArray*)bccArray         subject:(NSString*)subject         content:(NSString*)content    resultBlock:(void(^)(NSError*))resultBlock{    MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc]init];    smtpSession.hostname = hosName;    smtpSession.port = port;    smtpSession.username = userName;    smtpSession.password = password;    smtpSession.authType = (MCOAuthTypeSASLPlain | MCOAuthTypeSASLLogin);    smtpSession.connectionType = MCOConnectionTypeTLS;        //来自    MCOMessageBuilder *builder = [[MCOMessageBuilder alloc]init];    [[builder header] setFrom:[MCOAddress addressWithDisplayName:fromDisplayName mailbox:fromMaiBox]];        //接收    if(toArray){        NSMutableArray *to = [[NSMutableArray alloc]init];        for (NSString *item in toArray) {            [to addObject:[MCOAddress addressWithMailbox:item]];        }        [[builder header]setTo:to];    }    //抄送    if(ccArray){        NSMutableArray *cc = [[NSMutableArray alloc]init];        for (NSString *item in ccArray) {            [cc addObject:[MCOAddress addressWithMailbox:item]];        }        [[builder header]setTo:cc];    }        //密送    if(bccArray){        NSMutableArray *bcc = [[NSMutableArray alloc]init];        for (NSString *item in ccArray) {            [bcc addObject:[MCOAddress addressWithMailbox:item]];        }        [[builder header]setTo:bcc];    }        //主题    [[builder header]setSubject:subject];        //内容    [builder setHTMLBody:content];        NSData *rfc822Data = [builder data];    MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];    [sendOperation start:^(NSError *error) {        resultBlock(error);    }];}

这是给日本留学的同学写的一个Demo。


0 0
原创粉丝点击