Search code examples
iosobjective-cemailimapmailcore

how to fetch an email body in mailcore2


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?


Solution

  • 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];
        }];