MFMailComposeViewController发送邮件的实例
- iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.
- 项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>
- sendMailViewController.m文件的实现:
- - (void)viewDidLoad
- {
- UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- button.frame = CGRectMake(0, 40, 320, 50);
- [
- iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.
- 项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>
- sendMailViewController.m文件的实现:
- - (void)viewDidLoad
- {
- UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- button.frame = CGRectMake(0, 40, 320, 50);
- [button setTitle: @"Mail" forState: UIControlStateNormal];
- [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside];
- [self.view addSubview: button];
- }
- - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
- message:msg
- delegate:nil
- cancelButtonTitle:@"确定"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
- //点击按钮后,触发这个方法
- -(void)sendEMail
- {
- Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
- if (mailClass != nil)
- {
- if ([mailClass canSendMail])
- {
- [self displayComposerSheet];
- }
- else
- {
- [self launchMailAppOnDevice];
- }
- }
- else
- {
- [self launchMailAppOnDevice];
- }
- }
- //可以发送邮件的话
- -(void)displayComposerSheet
- {
- MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
- mailPicker.mailComposeDelegate = self;
- //设置主题
- [mailPicker setSubject: @"eMail主题"];
- // 添加发送者
- NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
- //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
- //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
- [mailPicker setToRecipients: toRecipients];
- //[picker setCcRecipients:ccRecipients];
- //[picker setBccRecipients:bccRecipients];
- // 添加图片
- UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
- NSData *imageData = UIImagePNGRepresentation(addPic); // png
- // NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
- [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
- NSString *emailBody = @"eMail 正文";
- [mailPicker setMessageBody:emailBody isHTML:YES];
- [self presentModalViewController: mailPicker animated:YES];
- [mailPicker release];
- }
- -(void)launchMailAppOnDevice
- {
- NSString *recipients = @"mailto:first@example.com&subject=my email!";
- //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
- NSString *body = @"&body=email body!";
- NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
- email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
- [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
- }
- - (void)mailComposeController:(MFMailComposeViewController *)controller
- didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
- {
- NSString *msg;
- switch (result)
- {
- case MFMailComposeResultCancelled:
- msg = @"邮件发送取消";
- break;
- case MFMailComposeResultSaved:
- msg = @"邮件保存成功";
- [self alertWithTitle:nil msg:msg];
- break;
- case MFMailComposeResultSent:
- msg = @"邮件发送成功";
- [self alertWithTitle:nil msg:msg];
- break;
- case MFMailComposeResultFailed:
- msg = @"邮件发送失败";
- [self alertWithTitle:nil msg:msg];
- break;
- default:
- break;
- }
- [self dismissModalViewControllerAnimated:YES];
- }
button setTitle: @"Mail" forState: UIControlStateNormal];
- [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside];
- [self.view addSubview: button];
- }
- - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
- message:msg
- delegate:nil
- cancelButtonTitle:@"确定"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
- //点击按钮后,触发这个方法
- -(void)sendEMail
- {
- Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
- if (mailClass != nil)
- {
- if ([mailClass canSendMail])
- {
- [self displayComposerSheet];
- }
- else
- {
- [self launchMailAppOnDevice];
- }
- }
- else
- {
- [self launchMailAppOnDevice];
- }
- }
- //可以发送邮件的话
- -(void)displayComposerSheet
- {
- MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
- mailPicker.mailComposeDelegate = self;
- //设置主题
- [mailPicker setSubject: @"eMail主题"];
- // 添加发送者
- NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
- //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
- //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
- [mailPicker setToRecipients: toRecipients];
- //[picker setCcRecipients:ccRecipients];
- //[picker setBccRecipients:bccRecipients];
- // 添加图片
- UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
- NSData *imageData = UIImagePNGRepresentation(addPic); // png
- // NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg
- [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
- NSString *emailBody = @"eMail 正文";
- [mailPicker setMessageBody:emailBody isHTML:YES];
- [self presentModalViewController: mailPicker animated:YES];
- [mailPicker release];
- }
- -(void)launchMailAppOnDevice
- {
- NSString *recipients = @"mailto:first@example.com&subject=my email!";
- //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
- NSString *body = @"&body=email body!";
- NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
- email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
- [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
- }
- - (void)mailComposeController:(MFMailComposeViewController *)controller
- didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
- {
- NSString *msg;
- switch (result)
- {
- case MFMailComposeResultCancelled:
- msg = @"邮件发送取消";
- break;
- case MFMailComposeResultSaved:
- msg = @"邮件保存成功";
- [self alertWithTitle:nil msg:msg];
- break;
- case MFMailComposeResultSent:
- msg = @"邮件发送成功";
- [self alertWithTitle:nil msg:msg];
- break;
- case MFMailComposeResultFailed:
- msg = @"邮件发送失败";
- [self alertWithTitle:nil msg:msg];
- break;
- default:
- break;
- }
- [self dismissModalViewControllerAnimated:YES];
- }
MFMailComposeViewController发送邮件的实例的更多相关文章
- MFMailComposeViewController发送邮件
1.iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面. 2.项目中需要添加MessageUi.framework.头文件加入 ...
- java发送邮件完整实例 java邮件工具类
http://yuncode.net/code/c_552a2e2dc593894 package com.srie.mail; import java.util.Properties; import ...
- python发送邮件的实例代码(支持html、图片、附件)
转自http://www.jb51.net/article/34498.htm 第一段代码 #!/usr/bin/python# -*- coding: utf-8 -*- import emaili ...
- php 发送邮件(实例)
html部分 <!DOCTYPE html> <html> <head> <title></title> <script type=& ...
- 使用ajax发送邮件的实例
jsp页面代码如下: <tr> <td> 发件人地址:<s:textfield id="fromAddress" name="fr ...
- 发送邮件(E-mail)方法整理合集
在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...
- iOS-----MFMessageCompose 和 MFMailComposeViewController的使用方法
MFMessageCompose 和 MFMailComposeViewController的使用方法 使用MFMessageComposeViewCOntroller发短信 应用想自己提供界面让用户 ...
- C# 使用windows服务发送邮件
最近做了一个使用 C# 写了一个发送邮件的 windows 服务,在这里记录一下. 首先使用 Visual Studio 2015 创建一个 windows 服务项目. 然后在设计器上面右击添加安装程 ...
- iOS开发-发送邮件(E-mail)方法整理合集(共3种)
前言:在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原 ...
随机推荐
- PAT Basic 1011
1011 A+B 和 C (15 分) 给定区间 [−231,231] 内的 3 个整数 A.B 和 C,请判断 A+B 是否大于 C. 输入格式: 输入第 1 行给出正整数 T (≤10 ...
- consul 文档
consul 服务发现 服务发现,用docker的时候可以使用,并且可以实现负载均衡. 因业务需要,所以留一下自己搜到比较好的资料吧 英文:https://www.consul.io/intro/ge ...
- sencha touch 学习汇总(转)
1.官方网站:http://www.sencha.com/products/touch/ 2.在线文档:http://docs.sencha.com/touch/2.2.1/ 3.在线翻译文档:htt ...
- js中字符串的方法
js String对象中常用方法小结,需要的朋友可以参考下: 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: ...
- Servlet(7)—ServletConfig接口和SevletContext接口
ServletConfig接口 1. 可以获取当前Servlet在web.xml中的配置信息(用的不多) 2. 在不使用"硬编码"的情况下,将部署状态信息传递给Servlet.这个 ...
- hdu1847 Good Luck in CET-4 Everybody!(巴什博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=1847 从1开始枚举情况,找规律.1先手胜2先手胜3先手败4先手胜5先手胜... n只要能转移到先手败,就可以实现 ...
- Quartz Cron表达式 每周、每月执行一次
原文:https://blog.csdn.net/qq_33432559/article/details/75633767 系统中通常有些需要自动执行的任务,这些任务可能每隔一段时间就要执行一次,也可 ...
- Oralce sql (+) 补充
Oracle 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制) 外连接(Outer Join) oute ...
- Mysql取随机数据效率测试(200W条中读取100条)
第一种方案: SELECT * FROM `follow_record` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(record_id) FROM ...
- 【问题与解决】showModalDialog is not defined 的解决方案
背景: showModalDialog 是比较老的方法了,有些浏览器不再支持弹出模态窗口了. 比如说谷歌浏览就不再支持了,有文章说明如下: Chrome’s Lack of Support for s ...