Aldee
Aldee

Reputation: 4538

Sending Email using mailto: URLs

Can someone help me with the following code? For sending email in iOS, is the below code a good one or should I use the MFMailComposeViewController than this?:

NSString *url = [NSString stringWithString: @"mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];NSString *url = [NSString stringWithString: @"mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

Is it a reliable code for sending mail?

Upvotes: 2

Views: 7900

Answers (3)

Julian B.
Julian B.

Reputation: 3725

To expand on the answers provided, I'd like to add that there is one benefit to the mailto approach, and that is that You don't really have to check if the user is able to send emails. If the user is not able to, it will prompt the user with the email wizard that will allow him/her to set up an email account with the default apple mail app.

In case of the MFMailComposeViewController, you should always check if the user can send emails with the canSendMail method, and act accordingly.

I'd like to also note that the mailto approach does not allow you to set a delegate in a straight forward way, making the error handling a little more tricky.

Upvotes: 2

DJPlayer
DJPlayer

Reputation: 3294

If this is targeted for IOS 3.0+ then MFMailCompseViewController

    #import <MessageUI/MFMailComposeViewController.h>
  //  ....

        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(@"sent");
        }
    [self dismissModalViewControllerAnimated:YES];
 }

Upvotes: 6

Ulrik
Ulrik

Reputation: 21

You really should use MFMailComposeViewController. It keeps you in the app and makes your code more readable.

Upvotes: 2

Related Questions