3

Please tell me how to send an email?

That means i have an uibutton when the user clicks that i want retrieve some data from app and send it to technical assistant`s mail id.. is there any simple method for doing this?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Anand
  • 3,346
  • 9
  • 35
  • 54
  • 3
    Please stop to tag your questions with xcode just because you are using this IDE to create your app. The Xcode tag should be used for questions regarding Xcode. – Matthias Bauch Apr 01 '11 at 12:43

4 Answers4

11

You have to use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol,

PeyloW provides the following code for this in his answer here:

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];
}
Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • dismissModalViewControllerAnimated - is Depricated please Use [self dismissViewControllerAnimated:NO completion:nil]; – Vicky Aug 18 '14 at 12:28
8

No need to create UIButton. You have to use the MFMailComposeViewController class and the MFMailComposeViewControllerDelegate protocol to send a mail..

For your easiness in creating e-mail app your can refer to the following link.

http://mobileorchard.com/new-in-iphone-30-tutorial-series-part-2-in-app-email-messageui/

it may be helpful to you in creating your app

Anand
  • 2,086
  • 2
  • 26
  • 44
2

If you want to send an email from your users email account you need to use the MFMailComposeViewController to present the user with a screen. You can't/shouldn't send emails from the users email account without their permission!

Otherwise you can start a normal SMTP connection to your server and use your own login details.

brain
  • 5,496
  • 1
  • 26
  • 29
2

I use the simple method of composing an email and opening it with the Mail app. This also works as a confirmation; if the user doesn't want to send it, he can just cancel. So no need to add an 'Are you sure?' popup. He can also add a note or such.

    NSString *mailurl=[NSString stringWithFormat:
@"mailto:%@?subject=%@%@&body=%@%@",mailaddr,mailsubject,
recipientname,mailmessage,mailsignature];

    [[UIApplication sharedApplication] openURL:
[NSURL URLwithString:[mailurl stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]]];

It works a charm and I use this method in several apps :)

Henrik Erlandsson
  • 3,797
  • 5
  • 43
  • 63