使用mailto:URL发送电子邮件

Ald*_*dee 2 cocoa cocoa-touch ios mfmailcomposeviewcontroller

有人可以帮助我使用以下代码吗?对于在iOS中发送电子邮件,下面的代码是好的还是我应该使用MFMailComposeViewController而不是这个?:

NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
Run Code Online (Sandbox Code Playgroud)

它是一个可靠的发送邮件的代码吗?

DJP*_*yer 6

如果这是针对IOS 3.0+然后MFMailCompseViewController

    #import <MessageUI/MFMailComposeViewController.h>
  //  ....

        MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;
        [controller setSubject:@"My Subject"];
        [controller setMessageBody:@"Hello there." isHTML:NO]; 
        if (controller) [self presentModalViewController:controller animated:YES];
        [controller release];
Run Code Online (Sandbox Code Playgroud)

然后用户完成工作,并及时获得委托回调:

 - (void)mailComposeController:(MFMailComposeViewController*)controller  
              didFinishWithResult:(MFMailComposeResult)result 
                            error:(NSError*)error;
 {
      if (result == MFMailComposeResultSent) {
          NSLog(@"sent");
        }
    [self dismissModalViewControllerAnimated:YES];
 }
Run Code Online (Sandbox Code Playgroud)