views:

25087

answers:

8

Hi All,

Good morning,

Can anybody help me out in following problem:

I want to send an Email from my iPhone application. I have heard about the fact that iPhone SDK doesn't have any mailing API to send mail. In this case, what are the different ways to achieve this facility. But i don't want to use the following code because it will exit my application, as iPhone only support one application at a time.

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

Thanks & Regards

Khushi

A: 

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

NSString *url = [NSString stringWithString: @"mailto:[email protected][email protected]&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.

leonho
While this works, this still quits the application, which is not desired per the asker.
Jeroen Heijmans
-1 the OP specifically stated he does not want this
ahmet emrah
+8  A: 

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.

Genericrich
+5  A: 

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

Handford
+6  A: 

little forgotten detail. The subject and body have to be url escaped, otherwise Mail.app won't launch. You can url escape via NSString's stringByAddingPercentEscapesUsingEncoding:

A: 

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

+2  A: 

A few things I'd like to add here:

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

  2. There is a limit to the length of the mailto URL. If the URL is larger than 4096 characters, mail.app won't launch.

  3. There is a new class in OS 3.0 that lets you send an e-mail without leaving your app. See the class MFMailComposeViewController.

+22  A: 

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.

Mugunth Kumar
Awesome post by Mugunth. Way to go buddy!
Jordan
Its really awesome. Thanks.I designed a view specially for accepting the email and subject from the user. by implementing the same code its again showing somewhat similar view. can i invoke the delegate method from my button press event in the view controller classThanks for your help,Shibin
Shibin Moideen
+34  A: 

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

First to send a message:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"My Subject"];
[controller setMessageBody:@"Hello there." isHTML:NO]; 
[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];
}
PeyloW
+1. The frameworks that need importing are mentioned here (http://www.mobileorchard.com/new-in-iphone-30-tutorial-series-part-2-in-app-email-messageui/).
Yar
To save you the jump, you need to #import <MessageUI/MFMailComposeViewController.h>
TomH