how to fetch an email body in mailcore2

大兔子大兔子 提交于 2019-11-29 22:41:25

问题


I've trying to migrate from mailcore to mailcore2. Previously in mailcore, fetching a body structure was as simple as [msg fetchBodyStructure] where msg is a CTCoreMessage object.

With mailcore2, things seem to be more complex. In MCOIMAPSession's documentation for fetching a message body, we have:

MCOIMAPFetchContentOperation * op = 
    [session fetchMessageAttachmentByUIDOperationWithFolder:@"INBOX"
                                                        uid:[message uid]
                                                     partID:"1.2"
                                                   encoding:[part encoding]];
 [op start:^(NSError * error, NSData * partData) {
 }];

I have absolutely no idea what this 1.2 is supposed to refer to. The authors refer the users to RFC 822, RFC 2822, and RFC 5322 but none of them has a straightforward answer to the above.

Can someone please show me a simple code sample of fetching an entire message body?


回答1:


I'm not sure why people complicate things: this is how you fetch email body with MailCore2:

MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
    [session setHostname:@"imap.gmail.com"];
    [session setPort:993];
    [session setUsername:[UICKeyChainStore stringForKey:@"username"]];
    [session setPassword:[UICKeyChainStore stringForKey:@"password"]];
    [session setConnectionType:MCOConnectionTypeTLS];
    MCOIMAPFetchContentOperation *operation = [session fetchMessageByUIDOperationWithFolder:@"INBOX" uid:message.uid];

    [operation start:^(NSError *error, NSData *data) {
        MCOMessageParser *messageParser = [[MCOMessageParser alloc] initWithData:data];

        NSString *msgHTMLBody = [messageParser htmlBodyRendering];
        [webView loadHTMLString:msgHTMLBody baseURL:nil];
    }];



回答2:


You want RFC 3501, part 6.4.5 - Fetch Command, specifically BODY[<section>]. The number refers to segments of the MIME structure of the message.

For example, if your messages mime structure looks like this, a common format:

multipart/alternative
|- text/plain
\- text/html

The parts and subparts are numbered recursively like this:

multipart/alternative (top-level)
|- text/plain (Part 1)
\- text/html (Part 2)

For a more complicated format, eg, with an attachment:

multipart/mixed (top-level)
|- multipart/alternative (Part 1)
|  |- text/plain (Part 1.1)
|  \- text/html (Part 1.2)
\- image/jpeg (Part 2)

If you want the entirety of the message, the part number is normally left blank. Or you can use TEXT if you want the entirety of the message, but without the headers.



来源:https://stackoverflow.com/questions/18034916/how-to-fetch-an-email-body-in-mailcore2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!