0

I want to start an email app from app on button pressed. But when I pressed the button nothing is happening !!!

code :

- (IBAction) startMail
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:emailAdress?subject=testMail&body=its test mail."]];
}

any thing wrong in code ? Also button is properly set in IB.

Thanks..

Maulik
  • 19,348
  • 14
  • 82
  • 137

6 Answers6

1

If you want to compose an EMail inside your app, you should have a look at the MFMailComposeViewController reference to do so instead of calling a mailto: URL scheme.

scalbatty
  • 778
  • 4
  • 12
  • how can I set the " to " field ? as data comes from previous view's table. and setToRecipients method requires the NSArray ! I have only one record (email address). – Maulik Apr 20 '11 at 09:50
  • use `[NSArray arrayWithObject:yourEmailAddressString]` to create your array – scalbatty Apr 20 '11 at 09:55
  • done ... thanks.. one more thing can I access the msg body ? I mean if I use setMessageBody to display default text and when I click on the body all default text should be cleared .. any idea ? – Maulik Apr 20 '11 at 10:06
  • This is not a supported behavior I guess. – scalbatty Apr 20 '11 at 10:19
  • and second thing when I click on saveDraft button then app gets crashed... how to handle it ? – Maulik Apr 20 '11 at 10:22
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.

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
1

You can use "MFMailComposeViewController" to send mail from your application.

Sample code-

MFMailComposeViewController *picker;

picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

NSString *objSubject = [[NSString alloc] init];<br/>
NSString *emailBody = [[NSString alloc] init];
[picker setSubject:objSubject];

[picker setMessageBody:emailBody isHTML:YES];

[self presentModalViewController:picker animated:YES];

You must use the following delegate to check the status of mail sent or not

**-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
}**

Enjoy

Vin
  • 10,517
  • 10
  • 58
  • 71
iphonedev23
  • 991
  • 2
  • 12
  • 24
0

Try this:

    NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@",
                        [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
                        [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
                        [body  stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];

Here to, subject and body are NSString variables

Vin
  • 10,517
  • 10
  • 58
  • 71
0

Check this out, it's a better solution than directing your user out of the app;
How can I send mail from an iPhone application

Community
  • 1
  • 1
ttarik
  • 3,824
  • 1
  • 32
  • 51
0
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]];
Vin
  • 10,517
  • 10
  • 58
  • 71
Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51