iOS SDK:如何调用电子邮件应用程序?

时间:2011-08-19 11:03:59

标签: iphone ios ipad email sdk

在我的iPad应用程序中,我想用自定义正文文本调用iPad的电子邮件应用程序。发件人和主题将为空,我想要设置的唯一参数是电子邮件的文本。我怎么能这样做?

谢谢!

5 个答案:

答案 0 :(得分:9)

为什么不在应用内打开电子邮件编辑器?

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

答案 1 :(得分:5)

在Apple的文档中查看MFMailComposeViewController。您可以像这样使用它:

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

不要忘记在#import <MessageUI/MessageUI.h>文件中添加.h。它将调用您委托中的方法,以便在取消或发送电子邮件(成功与否)时通知您。如果这对您有用,请告诉我。

答案 2 :(得分:5)

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

或者,正如Mihai建议的那样,请查看MFMailComposeViewController,它允许您在不离开应用的情况下发送邮件。

答案 3 :(得分:3)

以下方法用于向用户发送邮件。

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

答案 4 :(得分:0)

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