MailCore2 (一):基本的IMAP和SMTP的使用

1.基本的IMAP使用(源自Wiki)

使用MailCore 2在概念上比原来的MailCore稍微复杂一些。 MailCore 2中的所有提取请求都是通过队列异步进行的。这是什么意思呢?让我们看一个简单的例子:

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

#session称为会话控制是一次浏览器和服务器的交互的会话

 

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

    [session setPort:993];

    [session setUsername:@"[email protected]"];

    [session setPassword:@"123456"];

    [session setConnectionType:MCOConnectionTypeTLS];

 

    MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;

    NSString *folder = @"INBOX";   

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

 

    MCOIMAPFetchMessagesOperation *fetchOperation = [session fetchMessagesOperationWithFolder: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);  #打印邮件标题列表

    }];

在这个示例中,我们检索并打印了来自IMAP服务器的电子邮件标题列表 为了执行提取,我们用MCOIMAPSession实例从我们的参数中请求一个异步操作对象 当我们调用启动方法时,该操作对象能够启动与Gmail的连接。 现在,这里的事情有点棘手。 我们用一个Objective-C块调用启动函数,当获取操作完成时,它在主线程上执行。 IMAP的实际抓取是在后台线程上完成的,让UI和其他处理免费使用主线程。

通过IMAP更新标志

您可以更新MailCore2中的邮件标志,如下所示:

 BOOL deleted = NEW_FLAGS & MCOMessageFlagDeleted;

#满足NEW_FLAGS=1MCOMessageFlagDeleted =1,新标志才会包含'删除'

 

                MCOIMAPOperation *op = [session storeFlagsOperationWithFolder:@"INBOX"

                                                                 uids:[MCOIndexSet indexSetWithIndex:MESSAGE_UID]

                                                                 kind:MCOIMAPStoreFlagsRequestKindSet

                                                                 flags:NEW_FLAGS];

 

        [op start:^(NSError * error) {

            if(!error) {

                NSLog(@"Updated flags!");   #更新标志成功

            } else {

                NSLog(@"Error updating flags:%@", error);   #更新标志失败

            }

            

            if(deleted) {   #新标志包含'删除'

                MCOIMAPOperation *deleteOp = [session expungeOperation:@"INBOX"];

                [deleteOp start:^(NSError *error) {

                    if(error) {

                        NSLog(@"Error expunging folder:%@", error);#删除文件夹失败

                    } else {

                        NSLog(@"Successfully expunged folder"); #成功删除文件夹

                    }

                }];

            }

        }];

正如我们所看到的,我们为有问题的消息设置标志,如果新标志包含'删除'位,我们将删除该文件夹。



2.基本SMTP使用(源自Wiki)

为了使用MailCore 2发送消息,我们首先必须使用MCOMessageBuilder撰写该消息,然后我们可以将它传递给由SMTP会话生成的异步操作,以将该消息传递给SMTP服务器。 这里有一个简单的例子,假设你有一个电子邮件地址数组,用于/ cc / bcc:

MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];

smtpSession.hostname = @"smtp.gmail.com";

smtpSession.port = 465;

smtpSession.username = USERNAME;

smtpSession.password = PASSWORD;

smtpSession.connectionType = MCOConnectionTypeTLS;

 

MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];

[[builder header] setFrom:[MCOAddress addressWithDisplayName:nil mailbox:USERNAME]];NSMutableArray *to = [[NSMutableArray alloc] init];for(NSString *toAddress in RECIPIENTS) {  #字符串对象NSString

    MCOAddress *newAddress = [MCOAddress addressWithMailbox:toAddress];

    [to addObject:newAddress];

}

[[builder header] setTo:to];NSMutableArray *cc = [[NSMutableArray alloc] init];for(NSString *ccAddress in CC) {

    MCOAddress *newAddress = [MCOAddress addressWithMailbox:ccAddress];

    [cc addObject:newAddress];

}

[[builder header] setCc:cc];NSMutableArray *bcc = [[NSMutableArray alloc] init];for(NSString *bccAddress in BCC) {

    MCOAddress *newAddress = [MCOAddress addressWithMailbox:bccAddress];

    [bcc addObject:newAddress];

}

[[builder header] setBcc:bcc];

[[builder header] setSubject:SUBJECT];

[builder setHTMLBody:BODY];NSData * rfc822Data = [builder data];

 

MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];

[sendOperation start:^(NSError *error) {

    if(error) {

        NSLog(@"%@ Error sending email:%@", USERNAME, error);

    } else {

        NSLog(@"%@ Successfully sent email!", USERNAME);

    }

}];

// if you're running in a command line tool.// [[NSRunLoop currentRunLoop] run];

正如你所看到的,我们编写了这个消息,然后把我们的回调代码放入了开始块。


猜你喜欢

转载自blog.csdn.net/Torres_10/article/details/80289117
今日推荐