使用 mailto: URL 发送电子邮件

发布于 2024-12-29 01:49:50 字数 1101 浏览 0 评论 0原文

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

NSString *url = [NSString stringWithString: @"mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];NSString *url = [NSString stringWithString: @"mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

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

Can someone help me with the following code? For sending email in iOS, is the below code a good one or should I use the MFMailComposeViewController than this?:

NSString *url = [NSString stringWithString: @"mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];NSString *url = [NSString stringWithString: @"mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

Is it a reliable code for sending mail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆凄美了谁 2025-01-05 01:49:50

如果这是针对 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];

然后用户完成工作,您会及时收到委托回调:

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

If this is targeted for IOS 3.0+ then 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];

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(@"sent");
        }
    [self dismissModalViewControllerAnimated:YES];
 }
无需解释 2025-01-05 01:49:50

您确实应该使用 MFMailComposeViewController。它让您留在应用程序中并使您的代码更具可读性。

You really should use MFMailComposeViewController. It keeps you in the app and makes your code more readable.

倾城°AllureLove 2025-01-05 01:49:50

为了扩展所提供的答案,我想补充一点,mailto 方法有一个好处,那就是您实际上不必检查用户是否能够发送电子邮件。如果用户无法这样做,它将通过电子邮件向导提示用户,该向导将允许他/她使用默认的苹果邮件应用程序设置电子邮件帐户。

对于 MFMailComposeViewController,您应该始终检查用户是否可以使用 canSendMail 方法发送电子邮件,并采取相应措施。

我还想指出,mailto 方法不允许您以直接的方式设置委托,这使得错误处理变得更加棘手。

To expand on the answers provided, I'd like to add that there is one benefit to the mailto approach, and that is that You don't really have to check if the user is able to send emails. If the user is not able to, it will prompt the user with the email wizard that will allow him/her to set up an email account with the default apple mail app.

In case of the MFMailComposeViewController, you should always check if the user can send emails with the canSendMail method, and act accordingly.

I'd like to also note that the mailto approach does not allow you to set a delegate in a straight forward way, making the error handling a little more tricky.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文