MFMailComposeViewController发送邮件的实例

本文转载至 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: @"[email protected]"];
    51. //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
    52. //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]", 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:[email protected]&subject=my email!";
    69. //@"mailto:[email protected][email protected],[email protected]&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: @"[email protected]"];
  51. //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
  52. //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]", 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:[email protected]&subject=my email!";
  69. //@"mailto:[email protected][email protected],[email protected]&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. }
时间: 2024-08-25 12:49:10

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

MFMailComposeViewController发送邮件

1.iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面. 2.项目中需要添加MessageUi.framework.头文件加入MFMailComposeViewControllerDelegate.#import <MessageUI/MessageUI.h> - (void)viewDidLoad { // 实例化按钮用来调用邮箱 UIButton *button = [UIButton buttonWithType:

java发送邮件完整实例

实例一 import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.int

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

转自http://www.jb51.net/article/34498.htm 第一段代码 #!/usr/bin/python# -*- coding: utf-8 -*- import emailimport mimetypesfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMETextfrom email.MIMEImage import MIMEImageimport smtplib def

ThinkPHP5实现smtp发送邮件简易实例

进入thinkphp5根目录,使用composer安装phpmailer: composer require phpmailer/phpmailer 简单实例 <?php namespace app\common\event; class EMail { function send($to, $name, $subject = '', $body = '', $attachment = null) { $mail = new \PHPMailer\PHPMailer\PHPMailer(); /

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

MFMessageCompose 和 MFMailComposeViewController的使用方法 使用MFMessageComposeViewCOntroller发短信 应用想自己提供界面让用户输入短信收件人地址.短信内容.主体.附件等短信内容,则可使用MFMessageComposeViewController来发送短信,它也是一个视图控制器,继承UINavigationController. MFMessageComposeViewController提供了如下类方法判断iOS设备是否

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

前言:在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带有两种发送邮件的方法,另一种是使用第三方库: 1)openURL(原生) --用户体验较差,程序会进入后台,跳转至邮件发送界面. 2)MFMailComposeViewController(原生) --不会进入后台,使用模态弹出邮件发送视图. 3)SKPSMTPMessage(https://

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

前言:在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带有两种方法发送邮件的方法,另一种是使用第三方库: 1)openURL(原生) ——用户体验较差,程序会进入后台,跳转至邮件发送界面. 2)MFMailComposeViewController(原生) ——不会进入后台,使用模态弹出邮件发送视图. 3)SKPSMTPMessage(https:

邮件发送,无尽的501错误。TCP发送邮件解决方案

先贴上错误信息,便于搜索引擎采集,也送给遇到此问题的技术朋友们. smtp 501 Syntax error (no parameters allowed) (#5.5.4) 背景描述: 使用TCP发送邮件,在使用新浪邮箱进行smtp发送邮件时报出该错误(其他邮箱可能也存着该问题),QQ邮箱无该问题. ======================================================== 发送邮件其实很简单,对于像鄙人这种极限追求精简的人来说,肯定不乐意使用第三方邮件插

第五章:管理手机

一.使用AddressBook管理联系人 当我们的应用也需要访问甚至修改设备里的联系人信息,就需要借助于ABAddressBook或ABAddressBookUI来管理联系人信息,其中ABAddressBook只提供了一些工具函数来访问.修改联系人信息,而ABAddressBookUI则直接提供了一些试图控制器来访问.修改联系人信息.除此之外,系统还内置了打电话.发短信.发邮件的应用,这些应用为iPhone手机提供了最基本的功能. iPhone手机通常都有一个自带的Contacts应用,该应用用