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程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...
随机推荐
- python核心数据结构之字典
 [TOC ...
- [翻译]成为顶尖程序员应当学什么?Python、C还是Ruby?
原文地址(墙外):https://medium.com/life-tips/should-you-learn-python-c-or-ruby-to-be-a-top-coder-infographi ...
- 最近学习java时的记录
1.java 的变量一共分为三种,类变量,局部变量,成员变量 类变量就是 加static修饰符的变量 2.java 的修饰符可分为两大类,一 可访问修饰符 protected private publ ...
- (转载)JAVA中八种基本数据类型的默认值
原文链接: http://simon-c.iteye.com/blog/1016031 引用 For type byte, the default value is zero, that is, th ...
- Octopus——excel导入导出工具
Octopus Octopus是一个简易的Excel导入导出工具.目前主要就两个功能: 导入:将excel中一行数据转换为指定的java对象,并通过指定的正则表达式检查合法性. 导出:按照给定的xml ...
- iOS多线程开发之NSThread
一.NSThread基本概念 NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程,需要手动管理线程的生命周期,处理线程同 ...
- spring boot 拦截器添加
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Autowired private XxxInt ...
- 字符串(String)
字符串 字符串就是用引号引起来的一段文字.字母.数字-- 例如: "这是字符串"."zheshizifuc"."6666" 使用字符串的方法 ...
- jQuery点击缩略图切换大图代码
很多网站上都会有点击缩略图切换成大图的效果,下面来分享一下它的源码 还是先来看效果截图 运行文件 然后点击下一张 下面分享源代码 html文件 <!DOCTYPE html PUBLIC &qu ...
- docker~通过vs2017的Dockerfile来生成镜像
回到目录 Dockerfile这个东西我们之前是介绍过,它方便,快捷,易用,而在vs2017中也对docker进行了支持,而生成docker image的方式就是有用Dockerfile为基础的,在添 ...