首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
MFMailComposeViewController发送邮件的实例
时间:
2016-08-29 17:38:36
阅读:
131
评论:
0
收藏:
0
[点我收藏+]
标签:
本文转载至 http://blog.csdn.net/liufeng520/article/details/7585140
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发送邮件的实例
标签:
原文地址:http://www.cnblogs.com/Camier-myNiuer/p/5818792.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!