Sunday, June 3, 2012

How can I send mail from an iPhone application

I want to send an email from my iPhone application. I have heard that the iOS SDK doesn't have an email API. I don't want to use the following code because it will exit my application:

NSString *url = [NSString stringWithString: @"!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

So how can I send an email from my app?

Source: Tips4all


  1. On iOS 3.0 and later you should use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol, that that tucked away in the MessageUI framework.

    First add the framework and import:

    #import <MessageUI/MFMailComposeViewController.h>

    Then, to send a message:

    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"My Subject"];
    [controller setMessageBody:@"Hello there." isHTML:NO];
    if (controller) [self presentModalViewController:controller animated:YES];
    [controller release];

    Then the user does the work and you get the delegate callback in time:

    - (void)mailComposeController:(MFMailComposeViewController*)controller
    if (result == MFMailComposeResultSent) {
    NSLog(@"It's away!");
    [self dismissModalViewControllerAnimated:YES];

    Remember to check if the device is configured for sending email:

    if ([MFMailComposeViewController canSendMail]) {
    // Show the composer
    } else {
    // Handle the error

  2. MFMailComposeViewController is the way to go after the release of iPhone OS 3.0 software. You can look at the sample code or the tutorial I wrote.

  3. There is a project on google code to send messages through SMTP (specifically for the iPhone): skpsmtpmessage

  4. If you want to send email from your application, the above code is the only way to do it unless you code your own mail client (SMTP) inside your app, or have a server send the mail for you.

    For example, you could code your app to invoke a URL on your server which would send the mail for you. Then you simply call the URL from your code.

    Note that with the above code you can't attach anything to the email, which the SMTP client method would allow you to do, as well as the server-side method.

  5. A few things I'd like to add here:

    Using the mailto URL won't work in the simulator as isn't installed on the simulator. It does work on device though.
    There is a limit to the length of the mailto URL. If the URL is larger than 4096 characters, won't launch.
    There is a new class in OS 3.0 that lets you send an e-mail without leaving your app. See the class MFMailComposeViewController.

  6. You need to mailto: URL to launch the For example:

    NSString *url = [NSString stringWithString: @"!&body=Wish%20you%20were%20here!"];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

    Read iPhone URL Scheme Reference Document for more details on the parameters.

  7. hey I was looking for this same topic and found this Blog that talks about the how-to.