引入:
iPhone/iPad利用系统MFMailComposeViewController调用系统邮件,比如:在程序中点击按钮后,调出发送邮件的界面(MFMailComposeViewController实现). |
说明:
MFMailComposeViewController: 类提供了一个标准接口,它管理的编辑和发送电子邮件。您可以使用此视图控制器显示在您的应用程序标准的电子邮件意见,并填充与初始值,如主题,电子邮件收件人,正文和附件,查看字段。用户可以编辑您所指定的初始内容,并选择发送电子邮件或取消操作。 |
实现:
1、添加类库{Frameworks}:MessageUI.framework; 2、实现代码:
/* * @DO 发送邮件 * @param sender */ - (void) sendEmail:(id)sender { MFMailComposeViewController *mailCompose = [[MFMailComposeViewControlleralloc] init]; if(mailCompose) { //设置代理 [mailCompose setMailComposeDelegate:self]; NSArray *toAddress = [NSArrayarrayWithObject:@"****@***.com"]; NSArray *ccAddress = [NSArrayarrayWithObject:@"XXXX@XXX.com"]; NSString *emailBody = @"<H1>邮件内容!</H1>"; //设置收件人 [mailCompose setToRecipients:toAddress]; //设置抄送人 [mailCompose setCcRecipients:ccAddress]; //设置邮件内容 [mailCompose setMessageBody:emailBody isHTML:YES]; //设置邮件主题 [mailCompose setSubject:@"这里是主题"]; //设置邮件附件{mimeType:文件格式|fileName:文件名} [mailCompose addAttachmentData:imageData mimeType:@"image/jpg"fileName:@"1.png"]; //设置邮件视图在当前视图上显示方式 [selfpresentModalViewController:mailCompose animated:YES]; } [mailCompose release]; } /* * @DO 代理对邮件发送失败的处理 * @param controller * @param result * @param error */ - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { switch (result) { caseMFMailComposeResultCancelled: { UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"Send e-mail Cancel" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } break; case*********: { UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"E-mail have been saved" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } break; default: { UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"E-mail Not Sent" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } break; } //邮件视图消失 [selfdismissModalViewControllerAnimated:YES]; } | 说明:*****表示邮件发送失败的错误类型. | - |
国际化支持:
方式一、可以在项目的list属性文件中设置Localization native development region的属性值;
方式二、可以在调用MFMailComposeViewController的xib中设置Localization(方法如下:找到对应xib文件,右击该文件选择Get Infoà [General选项卡下单击{add Localization}])如台湾繁体{zh_TW}
总结:如果是调用MFMailComposeViewController为非xib形式,那就只能用方式一来支持国际化.