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: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];



So how can I send an email from my app?


Source: Tips4all

7 comments:

  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
    didFinishWithResult:(MFMailComposeResult)result
    error:(NSError*)error;
    {
    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
    }

    ReplyDelete
  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.

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

    ReplyDelete
  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.

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


    Using the mailto URL won't work in the simulator as mail.app 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, mail.app 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.

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

    NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];


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

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

    ReplyDelete