IOS MAILCore: how can I download email attachments and display them in a web view?

I am new to iOS development. I use MailCore to receive email in my application. My xcode version is 4.6.3 and iOS: 6.1 and 5.1 . Can anyone help me sort out my problem. I receive mail from my server with the number of connections and description, but I did not find a way to download this file or how I can get the attached file .

Please help me.

Thanks!

+4
source share
2 answers

It is very simple:

first of all - get uid's messages

MCOIndexSet *uidSet = [MCOIndexSet indexSetWithRange:MCORangeMake(1,UINT64_MAX)];//for all msgs MCOIMAPSession *session = <new or reuse IMAP session, i reuse this> MCOIMAPFetchMessagesOperation *fetchOp = [session fetchMessagesByUIDOperationWithFolder:@"INBOX" requestKind:MCOIMAPMessagesRequestKindFullHeaders uids:uidSet]; [fetchOp start:^(NSError *err, NSArray *messagesList, MCOIndexSet *vanished) { if (!err) { NSLog(@"Receive %i messages",(int)[messagesList count]); NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"header.date" ascending:NO]; //upd UI [[NSNotificationCenter defaultCenter] postNotificationName:kNotif_postCollector_ReceiveMessages object:nil]; //store msgs [[DataSingleton sharedSingleton] updateMessageListWithMessages:[messagesList sortedArrayUsingDescriptors:@[sort]]]; }else{ //oh no } }]; 

second - get the message structure (not everything, just necessary - the messagesList array)

 MCOIndexSet *uidSet = [MCOIndexSet indexSet]; for (Message *message in messagesList)// i use this class to store msgs. mcUid = MailCore Uid [uidSet addIndex:[[message mcUid] integerValue]]; MCOIMAPSession *session = <new or reuse IMAP session, i reuse this> MCOIMAPFetchMessagesOperation *fetchOp = [session fetchMessagesByUIDOperationWithFolder:@"INBOX"//or another folder requestKind:MCOIMAPMessagesRequestKindStructure uids:uidSet]; [fetchOp start:^(NSError *err, NSArray *messagesList, MCOIndexSet *vanished) { if (!err) { NSLog(@"Receive %i messages with structures",(int)[messagesList count]); NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"header.date" ascending:NO]; //store attachment precence [[DataSingleton sharedSingleton] updateAttachmentsPresenceForMessages:[messagesList sortedArrayUsingDescriptors:@[sort]] forAccount:acc]; /* You get array of this objects: MCOIMAPPart : MCOAbstractPart //A part identifier looks like 1.2.1 @property (nonatomic, copy) NSString * partID; //The size of the single part in bytes @property (nonatomic, nonatomic) unsigned int size; //It the encoding of the single part @property (nonatomic, nonatomic) MCOEncoding encoding; */ } }]; 

and step 3 and the last step:

 MCOIMAPSession *session = <new or reuse IMAP session, i reuse this> int uid = [[message mcUid] intValue]; NSString *partID = [attachment mcPartID];//we stored it on step 2 MCOIMAPFetchContentOperation * op = [session fetchMessageAttachmentByUIDOperationWithFolder:@"INBOX" uid:uid partID:partID encoding:(MCOEncoding)[[attachment mcEncoding] integerValue]]; NSLog(@"download att %i part %@",uid,partID); [op start:^(NSError * error, NSData * messageData) { if (error) { }else{ TRACE(@"receive att %i part %@",uid,partID); //save attachment fo local disc [[DataSingleton sharedSingleton] updateDownloadedAttachment:attachment withData:messageData]; } }]; 

Learn more about GitHub and the official libmailcore.com page .

+3
source
  MCOMessageParser * msg = [MCOMessageParser messageParserWithData:data]; if ([_message.attachments count] > 0) { for (int i=0; i<[_message.attachments count]; ++i) { MCOIMAPPart *part = [_message.attachments objectAtIndex:i]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDir, [part filename]]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; if (NO == fileExists) { [data writeToFile:filePath atomically:YES]; } } } 
0
source

Source: https://habr.com/ru/post/1493783/


All Articles