nosuic
nosuic

Reputation: 1360

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!

Upvotes: 6

Views: 5730

Answers (5)

venu reddy
venu reddy

Reputation: 1

NSString *textToShare = @"http:yourmail.com/";

NSArray *objectsToShare = @[textToShare];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,UIActivityTypeSaveToCameraRoll];

activityVC.excludedActivityTypes = excludeActivities;
[activityVC setValue:@"yourmail" forKey:@"subject"];

[self presentViewController:activityVC animated:YES completion:nil];

Upvotes: 0

ader
ader

Reputation: 5393

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

Upvotes: 9

Nimit Parekh
Nimit Parekh

Reputation: 16864

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

Upvotes: 3

omz
omz

Reputation: 53561

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.

Upvotes: 5

Mihai Fratu
Mihai Fratu

Reputation: 7663

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.

Upvotes: 5

Related Questions