How to work with Email in iOS app?

11,837

Solution 1

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

Solution 2

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];
}

Solution 3

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.

Solution 4

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 :)

Share:
11,837
Anand
Author by

Anand

Senior Mobile (iOS and Android) Application Developer

Updated on June 27, 2022

Comments

  • Anand
    Anand almost 2 years

    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?