本文转载至 http://blog.csdn.net/liufeng520/article/details/7585140
 
  1. iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.
  2. 项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>
  3. sendMailViewController.m文件的实现:
  4. - (void)viewDidLoad
  5. {
  6. UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  7. button.frame = CGRectMake(0, 40, 320, 50);
  8. [
    1. iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.
    2. 项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>
    3. sendMailViewController.m文件的实现:
    4. - (void)viewDidLoad
    5. {
    6. UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    7. button.frame = CGRectMake(0, 40, 320, 50);
    8. [button setTitle: @"Mail" forState: UIControlStateNormal];
    9. [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside];
    10. [self.view addSubview: button];
    11. }
    12. - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
    13. {
    14. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
    15. message:msg
    16. delegate:nil
    17. cancelButtonTitle:@"确定"
    18. otherButtonTitles:nil];
    19. [alert show];
    20. [alert release];
    21. }
    22. //点击按钮后,触发这个方法
    23. -(void)sendEMail
    24. {
    25. Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    26. if (mailClass != nil)
    27. {
    28. if ([mailClass canSendMail])
    29. {
    30. [self displayComposerSheet];
    31. }
    32. else
    33. {
    34. [self launchMailAppOnDevice];
    35. }
    36. }
    37. else
    38. {
    39. [self launchMailAppOnDevice];
    40. }
    41. }
    42. //可以发送邮件的话
    43. -(void)displayComposerSheet
    44. {
    45. MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    46. mailPicker.mailComposeDelegate = self;
    47. //设置主题
    48. [mailPicker setSubject: @"eMail主题"];
    49. // 添加发送者
    50. NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
    51. //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
    52. //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
    53. [mailPicker setToRecipients: toRecipients];
    54. //[picker setCcRecipients:ccRecipients];
    55. //[picker setBccRecipients:bccRecipients];
    56. // 添加图片
    57. UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
    58. NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    59. // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg
    60. [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
    61. NSString *emailBody = @"eMail 正文";
    62. [mailPicker setMessageBody:emailBody isHTML:YES];
    63. [self presentModalViewController: mailPicker animated:YES];
    64. [mailPicker release];
    65. }
    66. -(void)launchMailAppOnDevice
    67. {
    68. NSString *recipients = @"mailto:first@example.com&subject=my email!";
    69. //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
    70. NSString *body = @"&body=email body!";
    71. NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    72. email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    73. [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
    74. }
    75. - (void)mailComposeController:(MFMailComposeViewController *)controller
    76. didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    77. {
    78. NSString *msg;
    79. switch (result)
    80. {
    81. case MFMailComposeResultCancelled:
    82. msg = @"邮件发送取消";
    83. break;
    84. case MFMailComposeResultSaved:
    85. msg = @"邮件保存成功";
    86. [self alertWithTitle:nil msg:msg];
    87. break;
    88. case MFMailComposeResultSent:
    89. msg = @"邮件发送成功";
    90. [self alertWithTitle:nil msg:msg];
    91. break;
    92. case MFMailComposeResultFailed:
    93. msg = @"邮件发送失败";
    94. [self alertWithTitle:nil msg:msg];
    95. break;
    96. default:
    97. break;
    98. }
    99. [self dismissModalViewControllerAnimated:YES];
    100. }

    button setTitle: @"Mail" forState: UIControlStateNormal];

  9. [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside];
  10. [self.view addSubview: button];
  11. }
  12. - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
  13. {
  14. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
  15. message:msg
  16. delegate:nil
  17. cancelButtonTitle:@"确定"
  18. otherButtonTitles:nil];
  19. [alert show];
  20. [alert release];
  21. }
  22. //点击按钮后,触发这个方法
  23. -(void)sendEMail
  24. {
  25. Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
  26. if (mailClass != nil)
  27. {
  28. if ([mailClass canSendMail])
  29. {
  30. [self displayComposerSheet];
  31. }
  32. else
  33. {
  34. [self launchMailAppOnDevice];
  35. }
  36. }
  37. else
  38. {
  39. [self launchMailAppOnDevice];
  40. }
  41. }
  42. //可以发送邮件的话
  43. -(void)displayComposerSheet
  44. {
  45. MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
  46. mailPicker.mailComposeDelegate = self;
  47. //设置主题
  48. [mailPicker setSubject: @"eMail主题"];
  49. // 添加发送者
  50. NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
  51. //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
  52. //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
  53. [mailPicker setToRecipients: toRecipients];
  54. //[picker setCcRecipients:ccRecipients];
  55. //[picker setBccRecipients:bccRecipients];
  56. // 添加图片
  57. UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
  58. NSData *imageData = UIImagePNGRepresentation(addPic);            // png
  59. // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg
  60. [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
  61. NSString *emailBody = @"eMail 正文";
  62. [mailPicker setMessageBody:emailBody isHTML:YES];
  63. [self presentModalViewController: mailPicker animated:YES];
  64. [mailPicker release];
  65. }
  66. -(void)launchMailAppOnDevice
  67. {
  68. NSString *recipients = @"mailto:first@example.com&subject=my email!";
  69. //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
  70. NSString *body = @"&body=email body!";
  71. NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
  72. email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
  73. [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
  74. }
  75. - (void)mailComposeController:(MFMailComposeViewController *)controller
  76. didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
  77. {
  78. NSString *msg;
  79. switch (result)
  80. {
  81. case MFMailComposeResultCancelled:
  82. msg = @"邮件发送取消";
  83. break;
  84. case MFMailComposeResultSaved:
  85. msg = @"邮件保存成功";
  86. [self alertWithTitle:nil msg:msg];
  87. break;
  88. case MFMailComposeResultSent:
  89. msg = @"邮件发送成功";
  90. [self alertWithTitle:nil msg:msg];
  91. break;
  92. case MFMailComposeResultFailed:
  93. msg = @"邮件发送失败";
  94. [self alertWithTitle:nil msg:msg];
  95. break;
  96. default:
  97. break;
  98. }
  99. [self dismissModalViewControllerAnimated:YES];
  100. }

MFMailComposeViewController发送邮件的实例的更多相关文章

  1. MFMailComposeViewController发送邮件

    1.iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面. 2.项目中需要添加MessageUi.framework.头文件加入 ...

  2. java发送邮件完整实例 java邮件工具类

    http://yuncode.net/code/c_552a2e2dc593894 package com.srie.mail; import java.util.Properties; import ...

  3. python发送邮件的实例代码(支持html、图片、附件)

    转自http://www.jb51.net/article/34498.htm 第一段代码 #!/usr/bin/python# -*- coding: utf-8 -*- import emaili ...

  4. php 发送邮件(实例)

    html部分 <!DOCTYPE html> <html> <head> <title></title> <script type=& ...

  5. 使用ajax发送邮件的实例

    jsp页面代码如下: <tr>   <td>    发件人地址:<s:textfield id="fromAddress" name="fr ...

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

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

  7. iOS-----MFMessageCompose 和 MFMailComposeViewController的使用方法

    MFMessageCompose 和 MFMailComposeViewController的使用方法 使用MFMessageComposeViewCOntroller发短信 应用想自己提供界面让用户 ...

  8. C# 使用windows服务发送邮件

    最近做了一个使用 C# 写了一个发送邮件的 windows 服务,在这里记录一下. 首先使用 Visual Studio 2015 创建一个 windows 服务项目. 然后在设计器上面右击添加安装程 ...

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

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

随机推荐

  1. Python 调用图像融合API

    Python 调用图像融合API 本文记录使用Python,调用腾讯AI开放平台的图像融合API.官网给出的Demo用的是PHP,博主作为Python的粉丝,自然想用它来和『最好的』的语言一较高下,顺 ...

  2. JS_高程2.在HTML中使用Javascript(1)

    1.使用<script>元素向HTML页面中插入Javascript HTML4.01中<script>标签有6个属性: (1)async:可选.表示立即下载脚本,不影响页面中 ...

  3. itchat

    # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. "&quo ...

  4. Java全栈程序员之06:IDEA中MAVEN项目依赖及运行

    MAVEN已经成为事实上的企业项目开发中的项目类型.无论是IDEA还是Eclipse,都已经默认支持创建MAVEN项目.严格意义上来说,MAVEN不是一种新的JavaEE项目类型.它凌驾于所以的项目类 ...

  5. linux下rocksdb的编译安装

    RocksDB起源于Facebook的实验室项目,实现了一个高性能的快速存储器,是基于C++编写的key value数据库,很多软件都是采用内置rocksdb的方式运行,所以需要我们提前安装rocks ...

  6. [Canvas]空战游戏进阶 增加己方子弹管理类

    点此下载源码,可用Chrome打开观看. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta http ...

  7. 访问天地图WMTS服务的正确姿势

    天地图2018版对天地图应用开发流程进行了升级改造,主要有两点变化: (1)接口升级为tianditu.gov.cn政府域名,支持HTTP/HTTPS协议,原有服务域名tianditu.com继续保留 ...

  8. 内核中的锁机制--RCU

    一. 引言 众所周知,为了保护共享数据,需要一些同步机制,如自旋锁(spinlock),读写锁(rwlock),它们使用起来非常简单,而且是一种很有效的同步机制,在UNIX系统和Linux系统中得到了 ...

  9. lsof详解

    from:https://www.cnblogs.com/the-study-of-linux/p/5501593.html lsof (list open files)是一个列出当前系统打开文件的工 ...

  10. __NSArrayI __NSArray0 __NSSingleObjectArrayI __NSPlaceholderArray __NSArrayM

    如果你的 全局 可变数组 前面  用了  copy修饰,那么 调用   arr addObjectsFromArray: 的时候 就 会崩溃,而且提示你  是  [__NSArray0 addObje ...