Search code examples
emailmailcore2

Get host server name through email address in mail core2 framework for iOS


I want to know the default host name through the name of the email address....

I found a method in a class "MCONetService.h"....

hostnameWithEmail:

the reference link.. http://libmailcore.com/mailcore2/api/Classes/MCONetService.html

...but my problem is that i am unable to find the proper way to use this method because it is an instance method which requires the "MCONetService" class object to call that method,and i am getting null because it seems this object need some value before the use...

my code ...

MCONetService *netService=[[MCONetService alloc]init];        
[netService hostnameWithEmail:@"email@gmail.com"];

This is not a good way of coding but did't found any other way to try this method...

Any help will be appreciable....


Solution

  • Here's how to do it:

    First, make sure that you include providers.json in the resources of your app. Here's how to get the IMAP server related to a given email address.

    NSString * email = @"email@gmail.com";
    MCOMailProvider * provider = [[MCOMailProvidersManager sharedManager]
        providerForEmail:email];
    NSString * hostname = nil;
    if ([[provider imapServices] count] > 0) {
      MCONetService * service = [[provider imapServices] objectAtIndex:0];
      hostname = [service hostnameWithEmail:email];
    }
    if (hostname == nil) {
      NSLog(@"no IMAP server found");
    }
    else {
      NSLog(@"IMAP server: %@", hostname);
    }