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原 ...
随机推荐
- Python 调用图像融合API
Python 调用图像融合API 本文记录使用Python,调用腾讯AI开放平台的图像融合API.官网给出的Demo用的是PHP,博主作为Python的粉丝,自然想用它来和『最好的』的语言一较高下,顺 ...
- JS_高程2.在HTML中使用Javascript(1)
1.使用<script>元素向HTML页面中插入Javascript HTML4.01中<script>标签有6个属性: (1)async:可选.表示立即下载脚本,不影响页面中 ...
- itchat
# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. "&quo ...
- Java全栈程序员之06:IDEA中MAVEN项目依赖及运行
MAVEN已经成为事实上的企业项目开发中的项目类型.无论是IDEA还是Eclipse,都已经默认支持创建MAVEN项目.严格意义上来说,MAVEN不是一种新的JavaEE项目类型.它凌驾于所以的项目类 ...
- linux下rocksdb的编译安装
RocksDB起源于Facebook的实验室项目,实现了一个高性能的快速存储器,是基于C++编写的key value数据库,很多软件都是采用内置rocksdb的方式运行,所以需要我们提前安装rocks ...
- [Canvas]空战游戏进阶 增加己方子弹管理类
点此下载源码,可用Chrome打开观看. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta http ...
- 访问天地图WMTS服务的正确姿势
天地图2018版对天地图应用开发流程进行了升级改造,主要有两点变化: (1)接口升级为tianditu.gov.cn政府域名,支持HTTP/HTTPS协议,原有服务域名tianditu.com继续保留 ...
- 内核中的锁机制--RCU
一. 引言 众所周知,为了保护共享数据,需要一些同步机制,如自旋锁(spinlock),读写锁(rwlock),它们使用起来非常简单,而且是一种很有效的同步机制,在UNIX系统和Linux系统中得到了 ...
- lsof详解
from:https://www.cnblogs.com/the-study-of-linux/p/5501593.html lsof (list open files)是一个列出当前系统打开文件的工 ...
- __NSArrayI __NSArray0 __NSSingleObjectArrayI __NSPlaceholderArray __NSArrayM
如果你的 全局 可变数组 前面 用了 copy修饰,那么 调用 arr addObjectsFromArray: 的时候 就 会崩溃,而且提示你 是 [__NSArray0 addObje ...