Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SDK: How to invoke E-mail application?

From within my iPad application I would like to invoke iPad's E-mail app with custom body text. Senders and subject will be empty, the only parameter I would like to set is the text of the e-mail message. How could I do that?

Thanks!

like image 442
nosuic Avatar asked Aug 19 '11 11:08

nosuic


People also ask

What is iOS App SDK?

The iOS software development kit (iOS SDK) is a collection of tools for the creation of apps for Apple's mobile operating system.


4 Answers

Why not just open an email message composer inside your app?

MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

[mailController setSubject:@"my subject"];                  
[mailController setMessageBody:@"my message" isHTML:NO];

mailController.mailComposeDelegate = self;

UINavigationController *myNavController = [myViewController navigationController];

if ( mailController != nil ) {
    if ([MFMailComposeViewController canSendMail]){
        [myNavController presentModalViewController:mailController animated:YES];
    }
}

[mailController release];
like image 82
ader Avatar answered Oct 20 '22 18:10

ader


Take a look at MFMailComposeViewController in Apple's documentation. You can use it like this:

MFMailComposeViewController *controller=[[MFMailComposeViewController alloc]init];
controller.delegate = self;
[controller setMessageBody:<#yourBody#> isHTML:<#isHTML#>];
[self presentModalViewController:controller animated:YES];
[controller release];

Don't forget to add #import <MessageUI/MessageUI.h> in your .h file. It will call methods in your delegate to let you know when it was canceled or the email was sent (successfully or not). Let me know if that works for you.

like image 27
Mihai Fratu Avatar answered Oct 20 '22 19:10

Mihai Fratu


NSString *body = @"Hello Mail";
NSString *mailtoURLString = [NSString stringWithFormat:@"mailto:?body=%@", [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoURLString]];

Or, as Mihai suggested, take a look at MFMailComposeViewController which allows you to send mail without leaving your app.

like image 44
omz Avatar answered Oct 20 '22 19:10

omz


Following method is use for sending mail to user.

-(void)sendMail:(UIImage *)image
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    // Set the subject of email
    [picker setSubject:@"Picture from my iPhone!"];

    // Add email addresses  
    // Notice three sections: "to" "cc" and "bcc"
    [picker setToRecipients:[NSArray arrayWithObjects:@TO mailID1",@TO mailID2", nil]];
    [picker setCcRecipients:[NSArray arrayWithObject:@"CC MailID"]];
    [picker setBccRecipients:[NSArray arrayWithObject:@"BCC Mail ID"]];

    // Fill out the email body text
    NSString *emailBody = @"I just took this picture, check it out.";

    // This is not an HTML formatted email
    [picker setMessageBody:emailBody isHTML:NO];

    // Create NSData object as PNG image data from camera image
    NSData *data = UIImagePNGRepresentation(image);

    // Attach image data to the email   
    // 'CameraImage.png' is the file name that will be attached to the email 
    [picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"];

    // Show email view
    [self presentModalViewController:picker animated:YES];

    // Release picker   
    [picker release];
}
like image 42
Nimit Parekh Avatar answered Oct 20 '22 20:10

Nimit Parekh