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程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...
随机推荐
- pdf.js在国际化的时候,显示不了中文的解决办法
在项目中使用了pdf实现在线预览功能,开始工具栏中一直都是英文的,在view.js中设置了也不起作用,偶然发现了问题所在 当我把网站发布到iis上的时候,用google浏览器的审查元素功能的审核发现j ...
- iOS9新特性之常见关键字、泛型
#pragma mark -- nullable nullable:可以为空,只能修饰对象,不能修饰基本数据类型 // 方式一: @property (nonatomic, copy, nullabl ...
- 统计学习方法 三 kNN
KNN (一)KNN概念: K近邻算法是一种回归和分类算法,这主要讨论其分类概念: K近邻模型三要素: 1,距离: 2,K值的选择: K值选择过小:模型过复杂,近似误差减小,估计误差上升,出现过拟合 ...
- 前端如何将H5页面打包成本地app?
大家都知道H5页面怎么在手机浏览器里测试,但是如何能打包成APP测试呢?因为本人的手机是IOS系统,如果用Xcode打包需要开发证书,貌似99美元一年.意外发现一个叫Hbuild的软件,简单好用,下面 ...
- 关于引入多个jquery冲突的问题(附一个很好用的validate前端验证框架及使用方法)
废话不多说,进入正题: 如果一个jsp中想要使用两个不同版本的jquery怎么办呢?客官往下看: <script src="${ctxStatic}/jquery/jquery-1.8 ...
- LR监控Windows Server 2008 R2系统资源提示“指定的网络名不可用。”
问题现象: LR监控远程服务器Window Server 2008 R2 系统资源,提示“Monitor name :Windows Resources. Cannot connect to mach ...
- 2017寒假零基础学习Python系列之函数之 函数之定义可变参数
若想让函数接受任意个参数,就可以定义一个可变的参数: def fn(*args): print args fn() >>>() fn(1,2,5,6) >>>(1, ...
- readSerializableObj
package JBJADV003;import java.io.*;public class readSerializableObj { public static void main(String ...
- hdu2415(树上背包)
这道题好像没什么人写题解,于是写了一发 题意:有个坏蛋想要参加竞选,需要得到m个人的支持,买通第i个人(1<=i<=n)需要一个cost[i],同时这些人又有上下属关系,只要买通了领导,他 ...
- SSO(单点登录)与旅游年卡
SSO(单点登录)与旅游年卡 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应 ...