Using MailCore to Implement an Email Client in iOS .

Posted in: Blog

One of our clients asked us to implement Email Client in iOS. This was a new thing for our team, because we normally display the iOS default composer with preset values for sending Emails. For this project we were required to create an Email Client which could be configured with In-going and Out-going Servers.

Solution:

In order to implement the Email functionality in the iOS application we had two options:

1- Write a complete Email client which would send & receive the messages.
2- Find an existing library or code which would help us implement this functionality quickly.

We adopted the second approach, and after conducting our R&D, we found a C-based library (MailCore) which allowed us to setup the configurations for IMAP & SMTP.

It simply provides an asynchronous API to work with e-mail protocols (IMAP, POP and SMTP).

Here is the github link:
https://github.com/MailCore/mailcore2

It also has other features like:

• RFC parser and generator
• Asynchronous API
• HTML rendering of messages
• iOS and Mac support

Here is the Objective-C sample code which can be used as well:

Receive Email

MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
[session setHostname:@”imap.gmail.com”];
[session setPort:993];
[session setUsername:@”ADDRESS@gmail.com”];
[session setPassword:@”123456″];
[session setConnectionType:MCOConnectionTypeTLS];

MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;
NSString *folder = @”INBOX”;
MCOIndexSet *uids = [MCOIndexSet indexSetWithRange:MCORangeMake(1, UINT64_MAX)];

MCOIMAPFetchMessagesOperation *fetchOperation = [session fetchMessagesOperationWithFolder:folder requestKind:requestKind uids:uids];

[fetchOperation start:^(NSError * error, NSArray * fetchedMessages, MCOIndexSet * vanishedMessages) {
//We’ve finished downloading the messages!

//Let’s check if there was an error:
if(error) {
NSLog(@”Error downloading message headers:%@”, error);
}

//And, let’s print out the messages…
NSLog(@”The post man delivereth:%@”, fetchedMessages);
}];

Send Email

MCOSMTPSession *session = [[MCOSMTPSession alloc] init];
[session setHostname:@“smtp.gmail.com”];
[session setPort:465];
[session setUsername:@”ADDRESS@gmail.com”];
[session setPassword:@”123456″];
[session setConnectionType:MCOConnectionTypeTLS];

MCOMessgeBuilder *builder = [[MCOMessgeBuilder alloc] init];
[[builder header] setFrom:[MCOAdress addressWithDisplayName:@“DISPLAY_NAME” mailbox:@“CONTACT@gmail.com”]];
[[builder header] setTo:@[[MCOAdress addressWithMailbox:@“USERNAME”]]];
[[builder header] setSubject:@“DISPLAY_NAME”];
[builder setHTMLBody:@“EMAIL_BODY”];

MCOSMTPSendOperation *sendOperation = [session sendOperationWithData:[builder data]];

[sendOperation start:^(NSError * error) {
//We’ve finished sending the message!

//Let’s check if there was an error:
if(error) {
NSLog(@”Error sending message:%@”, error);
} else {//OR, If it was a success
NSLog(@“Successfully sent email!” );
}

}];

Leave a Reply

Your email address will not be published. Required fields are marked *