Search code examples
iosemailmailcore2

Using mailcore (iOS), how could I get all emails from a specific email address?


Basically, I have an app that the user enters his email address and password, the app saves it, and then the user enters an email address of a friend, and I need the app to get all inbox emails of the user where the friend is the sender. At first, I thought I would get all emails and then extract the ones for the friend- problem is, this takes a TON of time.. For a user with 4000 emails, it takes above ten minutes.

MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
session.hostname = @"imap.gmail.com";
session.port = 993;
session.username = @"maor.kern@gmail.com";
session.password = @"xxxx";
session.connectionType = MCOConnectionTypeTLS;

MCOIndexSet *uidSet = [MCOIndexSet indexSetWithRange:MCORangeMake(1,UINT64_MAX)];
MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindFullHeaders;

MCOIMAPFetchMessagesOperation *fetchOp =
[session fetchMessagesByUIDOperationWithFolder:@"INBOX"
                                   requestKind:requestKind
                                          uids:uidSet];

[fetchOp start:^(NSError *err, NSArray *msgs, MCOIndexSet *vanished) {

    for (int i = 0; i < [msgs count]; i++) {
        MCOIMAPMessage *m = msgs[i];

        MCOIMAPFetchContentOperation *operation = [session fetchMessageByUIDOperationWithFolder:@"INBOX" uid:m.uid];

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

            NSString *msgHTMLBody = [messageParser htmlBodyRendering];
            NSLog(@"%i", i);
        }];
    }

}];

So, I really need some way to get all emails from a specific person. Is this possible? And if so, how could I do this? Thanks!


Solution

  • Use *+ (MCOIMAPSearchExpression *) searchFrom:(NSString )value

    to get the list of messageUids with the fromAddress being the search result and then Fetch the mails of these messageUid List.