本文转载至 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. input的一些使用方法

  2. Python中将array类型不按科学计数法存在文件中的方法

    直接上代码: from numpy import *import numpy as npDrug_array = zeros((708,708),dtype = int)f = open('D:\ma ...

  3. Windows平台下SVN安装配置及使用

    原文链接:https://www.cnblogs.com/snake-hand/archive/2013/06/09/3130022.html,等有空了玩一玩吧,现在没空.

  4. Annotation 的第一个工程

    一.什么是 Annotation? java.lang.annotation,接口 Annotation.对于Annotation,是Java5的新特性,JDK5引入了Metadata(元数据)很容易 ...

  5. C#_02.13_基础三_.NET类基础

    C#_02.13_基础三_.NET类基础 一.类概述: 类是一个能存储数据和功能并执行代码的数据结构,包含数据成员和函数成员.(有什么和能够干什么) 运行中的程序是一组相互作用的对象的集合. 二.为类 ...

  6. poj2456 Aggressive cows(二分查找)

    https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...

  7. Go语言之高级篇beego框架之layui框架应用

    1.layui前端框架 参考地址:https://www.layui.com

  8. JAVA调用外部安装7-Zip压缩和解压zip文件

    1.首先在本地安装7-Zip(下载链接:https://www.7-zip.org/)2.调用7-Zip压缩.zip文件: /**      * 生成.zip压缩文件      * @param fi ...

  9. float 浮点数与零值0比较大小 ZZ

    float x: 千万不要写x==0; 写出float x 与“零值”比较的if语句——一道面试题分析 写出float  x 与“零值”比较的if语句 请写出 float  x 与“零值”比较的 if ...

  10. mybatis 批量添加

    <insert id="addTrackBatch" parameterType="java.util.List"> INSERT INTO t_t ...