如何从iPhone应用程序发送邮件 [英] How can I send mail from an iPhone application

查看:129
本文介绍了如何从iPhone应用程序发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的iPhone应用程式传送电子邮件。我听说iOS SDK没有电子邮件API。我不想使用下面的代码,因为它将退出我的应用程序:

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

那么如何从我的应用程序发送电子邮件?

MFMailComposeViewController 类和 MFMailComposeViewControllerDelegate / code>协议,这是在MessageUI框架中。



首先添加框架并导入:

  #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(@It's away!
}
[self dismissModalViewControllerAnimated:YES];
}

请记得检查设备是否配置为发送电子邮件:

  if([MFMailComposeViewController canSendMail]){
//显示作曲家
} else {
//处理错误
}


I want to send an email from my iPhone application. I have heard that the iOS SDK doesn't have an email API. I don't want to use the following code because it will exit my application:

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

So how can I send an email from my app?

解决方案

On iOS 3.0 and later you should use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol, that is tucked away in the MessageUI framework.

First add the framework and import:

#import <MessageUI/MFMailComposeViewController.h>

Then, to send a message:

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(@"It's away!");
  }
  [self dismissModalViewControllerAnimated:YES];
}

Remember to check if the device is configured for sending email:

if ([MFMailComposeViewController canSendMail]) {
  // Show the composer
} else {
  // Handle the error
}

这篇关于如何从iPhone应用程序发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆