Changes From The Original MailCore

Robert Widmann edited this page Sep 13, 2013 · 2 revisions

MailCore2 brings an entirely newer, faster, C++ based, API that can be quite a shock coming from the original version of MailCore. A lot of constructs have either been replaced outright, or have changed in the way in which they are used.

What's New?

  • Entirely Asynchronous Requests
  • Session-based Email Handling
  • POP
  • Search
  • Email Providers

Session, what's a Session?

Sessions have always been a part of MailCore, but MailCore2 makes them first class citizens. MailCore2 introduces the concept of sessions for IMAP and POP transactions instead of accounts. In doing so, we feel that it forces the programmer to decouple the implementation of any email logic from a credentials store (which is essentially all an account object should be). Sessions are also a more powerful and customizable way of performing actions associated with an account.

For instance, to fetch a list of the folders associated with an account in MailCore, you might have code like this:

- (void)fetchFolders {
    CTCoreAccount *myAccount = [[CTCoreAccount alloc] init];
    [myAccount connectToServer:@"imap.gmail.com"
                          port:993
                connectionType:CONNECTION_TYPE_PLAIN
                      authType:IMAP_AUTH_TYPE_PLAIN
                         login:@"email@gmail.com"
                      password:@"1234567890"]; //Ultra-secure password
    NSLog(@"All folders for account %@", myAccount.allFolders);
}

MailCore2 instead allows you to perform a non-blocking operation off the main thread:

- (void)fetchFolders {
    MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
    [session setHostname:@"imap.gmail.com"];
    [session setPort:993];
    [session setUsername:@"email@gmail.com"];
    [session setPassword:@"1234567890"];
    [session setConnectionType:MCOConnectionTypeTLS];
    [[session fetchAllFoldersOperation]start:^(NSError * error, NSArray * /* MCOIMAPFolder */ folders) {
        NSLog(@"All folders for session %@", folders);
    }];
}

Providers

MailCore did not provide a way to hand over an email and automatically receive credentials about the associated server. MailCore2 calls these providers, and they are an easy way to seamlessly transition away from having the user provide ports or hostnames. MailCore2 provides a pre-made list of providers, that can be searched with a given email address like so:

- (void)registerAccountWithEmail:(NSString*) email {
    MCOMailProvider *accountProvider = [MCOMailProvidersManager.sharedManager providerForEmail:email];
    if (!accountProvider) {
        NSLog(@"No provider available for email: %@", email);
        return;
    }
    //Check if the account provides you with IMAP services
    NSArray *imapServices = accountProvider.imapServices;
    if (imapServices.count != 0) [
        MCONetService *imapService = imapServices[0];
        MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
        [session setHostname:imapService.hostname];
        [session setPort:imapService.port];
        [session setUsername:email];
        [session setPassword:@"1234567890"];
        [session setConnectionType:imapService.connectionType];
    }
}

New providers can even be created with a JSON file following our formatting rules and using either MailProvidersManager::registerProviders() or [MCOMailProvidersManager registerProvidersWithFilename:].

*Note that while providers do give you a list of services and a list of folders by default, that does not excuse you from fetching a list of folders or validating those services yourself.