ios发送邮件
方法一:
1.需要引入库MessageUI.framework
#import <MessageUI/MessageUI.h>
#import<MessageUI/MFMailComposeViewController.h>
2.@interface ViewController : UIXXXXXViewController <..., MFMailComposeViewControllerDelegate>
@end
3.发送执行代码。事先验证相关支持。
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (!mailClass) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"发送邮件"
message:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"
delegate:self
cancelButtonTitle:@"我知道啦"
otherButtonTitles: nil] autorelease];
[alert show]; return;
}
if (![mailClass canSendMail]) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"发送邮件"
message:@"用户没有设置邮件账户"
delegate:self
cancelButtonTitle:@"我知道啦"
otherButtonTitles: nil] autorelease];
[alert show];
return;
} MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:@"Hello, World!"];
[mc setToRecipients:[NSArray arrayWithObject:@"xxxxx@163.com"]];
// [mc setCcRecipients:[NSArray arrayWithObject:@"xxxxx@163.com"]];
// [mc setBccRecipients:[NSArray arrayWithObject:@"secret@gmail.com"]];
[mc setMessageBody:@"Hello,slick!!!\n\nCome here, I need you!" isHTML:NO]; // 添加一张图片
UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
NSData *imageData = UIImagePNGRepresentation(addPic); // png
[mc addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"]; //添加一个pdf附件
NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
NSData *pdf = [NSData dataWithContentsOfFile:file];
[mc addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"]; [self presentViewController:mc animated:YES completion:nil];
[mc release];
回调函数:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error {
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail send canceled...");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved...");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent...");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail send errored: %@...", [error localizedDescription]);
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
方法二:
url方式
#pragma mark - 使用系统邮件客户端发送邮件
-(void)launchMailApp
{
NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
[mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
[mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
[mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
//添加主题
[mailUrl appendString:@"&subject=my email"];
//添加邮件内容
[mailUrl appendString:@"&body=<b>email</b> body!"];
NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
即 [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"]];
还可使用skpsmtpmessage这样的第三方控件。
ios发送邮件的更多相关文章
- iOS - 发送邮件
IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController.借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能. ...
- iOS开发-发送邮件(E-mail)方法整理合集(共3种)
前言:在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原 ...
- 发送邮件(E-mail)方法整理合集
在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...
- ios 后台发送邮件之SKPSMTPMessage的使用
skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git 1.由于skpsmtpmessage是非ARC ...
- [原]IOS 后台发送邮件
skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git -(void)statrUpLoad:(id) ...
- iOS调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的
在iOS开发中,经常需要调用其它App,如拨打电话.发送邮件等.UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的模式来调用不同的App. 通 ...
- iOS中发送短信/发送邮件的实现 韩俊强的博客
需要引入框架: MessageUI.framework 布局如下: 短信和邮件: #import "ViewController.h" #import <MessageUI/ ...
- 47.iOS跳转AppStore评分和发送邮件
1.跳转到AppStore评分 应用地址是关键:IOS 设备,手机搜索应用,拷贝链接 NSString *appStr =@"https://itunes.apple.com/cn/app/ ...
- mono中发送邮件并保存本次收件人的地址
在ios端mono开发中,发送邮件可以选择调用ios原生email程序.有两种方式实现这种功能,一是程序跳转到ipad中email程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...
随机推荐
- Generic(泛型)
什么是泛型:"通过参数化类型来实现在同一份代码上操作多种数据类型.利用"参数化类型"将类型抽象化,从而实现灵活的复用". 简单来说泛型就是为了使一些代码能够重复 ...
- AngularJS模块
方式一: <body ng-app="myApp"> <div ng-controller="myCtrl1"> <h1>{ ...
- Example017简单的下拉框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- xUtils使用详细介绍
xUtils3使用详解 一.xUtils简介: xUtils是基于Afinal开发的目前功能比较完善的一个Android开源框架,官网:https://github.com/wyouflf/xUtil ...
- 通过history解决ajax不支持前进/后退/刷新
前言: 现在前后端基本都是通过ajax实现前后端接口数据的交互,但是,ajax有个小小的劣势,即:不支持浏览器“后退”和“前进“键. 但是,现在我们可以通过H5的histroy属性 解决ajax在交互 ...
- Java泛型概念
1. 概述在引入范型之前,Java类型分为原始类型.复杂类型,其中复杂类型分为数组和类.引入范型后,一个复杂类型就可以在细分成更多的类型.例如原先的类型List,现在在细分成List<Objec ...
- OJ2236“孤单数”题目报告
题目描述:有2n+1个数,其中有n对数字是成双出现的,有且仅有1个数字只有它自己一个.请你找出这个孤单数. 输入描述: 第一行有且只有一个正整数n(n<=500000) 第二行有2n+1个数ai ...
- ORM的概念, ORM到底是什么
一.ORM简介 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.简单的说,ORM是通过使 ...
- MySQL学习笔记(三)
--回顾 字段类型(列类型):数值型,时间日期型和字符串类型 数值型:整型和小数型(浮点型和定点型) 时间日期型:datetime,date,time,timestamp,year 字符串类型:定长, ...
- ubuntu(linux)新装系统恢复备份后无法进入系统问题的解决
1. 引子: 可以跳过直接看3.~5. 我们可能会遇到这样的状况,从ubuntu转到别的系统,兜兜转转又回到了ubuntu. 比如说: Fedora升级后一大堆BUG,与java有关的软件渲染都糟糕的 ...