IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController。借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能。

1.openURL

使用openURL调用系统邮箱客户端是我们在IOS3.0以下实现发邮件功能的主要手段。我们可以通过设置url里的相关参数来指定邮件的内容,不过其缺点很明显,这样的过程会导致程序暂时退出。下面是使用openURL来发邮件的一个小例子:
  1. #pragma mark - 使用系统邮件客户端发送邮件
  2. -(void)launchMailApp
  3. {
  4. NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];
  5. //添加收件人
  6. NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
  7. [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
  8. //添加抄送
  9. NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
  10. [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
  11. //添加密送
  12. NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
  13. [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
  14. //添加主题
  15. [mailUrl appendString:@"&subject=my email"];
  16. //添加邮件内容
  17. [mailUrl appendString:@"&body=<b>email</b> body!"];
  18. NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  19. [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
  20. }

2.MFMailComposeViewController

MFMailComposeViewController是在IOS3.0新增的一个接口,它在MessageUI.framework中。通过调用MFMailComposeViewController,可以把邮件发送窗口集成到我们的应用里,发送邮件就不需要退出程序了。MFMailComposeViewController的使用方法:
  • 1.项目中引入MessageUI.framework;
  • 2.在使用的文件中导入MFMailComposeViewController.h头文件;
  • 3.实现MFMailComposeViewControllerDelegate,处理邮件发送事件;
  • 4.调出邮件发送窗口前先使用MFMailComposeViewController里的“+ (BOOL)canSendMail”方法检查用户是否设置了邮件账户;
  • 5.初始化MFMailComposeViewController,构造邮件体
  1. //
  2. //  ViewController.h
  3. //  MailDemo
  4. //
  5. //  Created by LUOYL on 12-4-4.
  6. //  Copyright (c) 2012年 http://luoyl.info. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import <MessageUI/MFMailComposeViewController.h>
  10. @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
  11. @end
 
  1. #pragma mark - 在应用内发送邮件
  2. //激活邮件功能
  3. - (void)sendMailInApp
  4. {
  5. Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
  6. if (!mailClass) {
  7. [self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];
  8. return;
  9. }
  10. if (![mailClass canSendMail]) {
  11. [self alertWithMessage:@"用户没有设置邮件账户"];
  12. return;
  13. }
  14. [self displayMailPicker];
  15. }
  16. //调出邮件发送窗口
  17. - (void)displayMailPicker
  18. {
  19. MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
  20. mailPicker.mailComposeDelegate = self;
  21. //设置主题
  22. [mailPicker setSubject: @"eMail主题"];
  23. //添加收件人
  24. NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
  25. [mailPicker setToRecipients: toRecipients];
  26. //添加抄送
  27. NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
  28. [mailPicker setCcRecipients:ccRecipients];
  29. //添加密送
  30. NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
  31. [mailPicker setBccRecipients:bccRecipients];
  32. // 添加一张图片
  33. UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
  34. NSData *imageData = UIImagePNGRepresentation(addPic);            // png
  35. //关于mimeType:http://www.iana.org/assignments/media-types/index.html
  36. [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
  37. //添加一个pdf附件
  38. NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
  39. NSData *pdf = [NSData dataWithContentsOfFile:file];
  40. [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
  41. NSString *emailBody = @"<font color='red'>eMail</font> 正文";
  42. [mailPicker setMessageBody:emailBody isHTML:YES];
  43. [self presentModalViewController: mailPicker animated:YES];
  44. [mailPicker release];
  45. }
  46. #pragma mark - 实现 MFMailComposeViewControllerDelegate
  47. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
  48. {
  49. //关闭邮件发送窗口
  50. [self dismissModalViewControllerAnimated:YES];
  51. NSString *msg;
  52. switch (result) {
  53. case MFMailComposeResultCancelled:
  54. msg = @"用户取消编辑邮件";
  55. break;
  56. case MFMailComposeResultSaved:
  57. msg = @"用户成功保存邮件";
  58. break;
  59. case MFMailComposeResultSent:
  60. msg = @"用户点击发送,将邮件放到队列中,还没发送";
  61. break;
  62. case MFMailComposeResultFailed:
  63. msg = @"用户试图保存或者发送邮件失败";
  64. break;
  65. default:
  66. msg = @"";
  67. break;
  68. }
  69. [self alertWithMessage:msg];
  70. }

iOS - 发送邮件的更多相关文章

  1. ios发送邮件

    方法一: 1.需要引入库MessageUI.framework #import <MessageUI/MessageUI.h> #import<MessageUI/MFMailCom ...

  2. iOS开发-发送邮件(E-mail)方法整理合集(共3种)

    前言:在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原 ...

  3. 发送邮件(E-mail)方法整理合集

    在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...

  4. ios 后台发送邮件之SKPSMTPMessage的使用

    skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git 1.由于skpsmtpmessage是非ARC ...

  5. [原]IOS 后台发送邮件

    skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git -(void)statrUpLoad:(id) ...

  6. iOS调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的

    在iOS开发中,经常需要调用其它App,如拨打电话.发送邮件等.UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的模式来调用不同的App. 通 ...

  7. iOS中发送短信/发送邮件的实现 韩俊强的博客

    需要引入框架: MessageUI.framework 布局如下: 短信和邮件: #import "ViewController.h" #import <MessageUI/ ...

  8. 47.iOS跳转AppStore评分和发送邮件

    1.跳转到AppStore评分 应用地址是关键:IOS 设备,手机搜索应用,拷贝链接 NSString *appStr =@"https://itunes.apple.com/cn/app/ ...

  9. mono中发送邮件并保存本次收件人的地址

    在ios端mono开发中,发送邮件可以选择调用ios原生email程序.有两种方式实现这种功能,一是程序跳转到ipad中email程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...

随机推荐

  1. Android Studio 完美修改应用包名

    我们平时新建项目有些朋友可能当时就是随意写的一个包名,然后在项目过程中, 又感觉这个包名不太好,所以就要对包名进行修改,根据我们正常的修改方式,是这样的. 在种情况是只能修改最外层的那个名称, 如果我 ...

  2. Android 读取手机短信

    获取android手机短信需要在AndroidManifest.xml加权限: <uses-permission android:name="android.permission.RE ...

  3. Thumb

    这个控件,真不好介绍,MSDN上也是草草几句,反正就是可以让用户拖动的玩意儿,但是,你会发现,当你在该控件上拖动时,它没有反响,也就是说这个东西默认不做任何操作的,它是赖在那里什么都不干,除非你去踢上 ...

  4. mysql的having语句

    mysql> use qq; Database changed mysql> #查询本店价比市场价省的钱,并且要求省钱200元以上的取出来 mysql> select goods_i ...

  5. ASP.NET MVC(三) TypeScript

    TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScript与JavaScript相比 ...

  6. angularjs + springmvc 上传和下载

    jsp: <form ng-submit="uploadFile()" class="form-horizontal" enctype="mul ...

  7. oracle 11g 64位安装sqldeveloper打开不了

    oracle 11g 64位安装sqldeveloper打开不了解决方法: 1.到官网下载对应版本的sqldeveloper. 2.找对应安装路径下的F:\app\Administrator\prod ...

  8. oc for in 的时候nsscanner: nil string argument

    今天偶然发现,oc for in 动态的给一数组加东西,然后嵌套for in 会报nsscanner: nil string argument. 换成for循环就好了,暂时还没找到原因

  9. iOS 计算两个日期之间的天数问题

    //获取当前时间若干年.月.日之后的时间 + (NSDate *)dateWithFromDate:(NSDate *)date years:(NSInteger)years months:(NSIn ...

  10. Http上传文件

    public class UpLoadFile { public static void UpLoadFiles(string fileName) { string fileType = Path.G ...