MailCore2 使用

                            MailCore2 使用

一、IMAP 接收邮件 1.设置账号:

1)分别设置MCOIMAPSession AAA属性:

MCOIMAPSession *session = [[MCOIMAPSession alloc] init];

[session setHostname:@"imap.gmail.com"];

[session setPort:993];

[session setUsername:@"ADDRESS@gmail.com"];

[session setPassword:@"123456"];

[session setConnectionType:MCOConnectionTypeTLS];


记录session可通过AAA的connectionLogger属性,具体如下:

[session setConnectionLogger:^(void * connectionID, MCOConnectionLogType type, NSData * data) {

NSLog(@"%@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);

// ...}];

2)设置MCOIMAPOperation BBB:

BBB = [AAA checkAccountOperation];

开启openation获取信息:

[BBB start:^(NSError *error) {

    //请求详细邮件内容

}];

3)请求详细邮件内容:

MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;

NSString *folder = @"INBOX";

MCOIndexSet *uids = [MCOIndexSet indexSetWithRange:MCORangeMake(1, UINT64_MAX)];


MCOIMAPFetchMessagesOperation *fetchOperation = [session fetchMessagesByUIDOperationWithFolder:folder requestKind:requestKind uids:uids];


[fetchOperation start:^(NSError * error, NSArray * fetchedMessages, MCOIndexSet * vanishedMessages) {

    //We've finished downloading the messages!


    //Let's check if there was an error:

    if(error) {

        NSLog(@"Error downloading message headers:%@", error);

    }


    //And, let's print out the messages...

    NSLog(@"The post man delivereth:%@", fetchedMessages);

}];

二、SMTP发送邮件

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

}

}];

P.S. Sample Code: https://github.com/MailCore/mailcore2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值