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程序,另外一种是将发送邮件的界面在自己应用里弹出. 首先第一种方式的代 ...
随机推荐
- 【LeetCode】205. Isomorphic Strings
题目: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the c ...
- 百度搜索效果(jsonp法)
百度搜索效果(jsonp法): 不需要放在服务器中,本地就可以执行 <!DOCTYPE html> <html> <head> <meta charset=& ...
- create groups 和 create folder reference
当将文件拖入工程中的时候会出现这个对话框,这个对话框中在Added folders中有两种选择:Create groups 和 Create folder references 这两种的区别是 ...
- C#设置WebBrowser默认浏览器
由于VS的WebBrowser控件的默认浏览器是IE7,好多网页兼容性不是很好,所以要修改下默认浏览器. 设置前: 设置后: 在WebBrowser界面加载时执行以下方法,设置浏览器. /// ...
- Linux 可重入内核
Linux内核是可重入的,这意味着几个进程可能同时在内核模式下执行.(当然单处理器系统,在某一时间只会有一个进程执行,但许多会阻塞在内核模式)这些进程会分时共享CPU.I/O设备等系统资源,给用户的感 ...
- poj 1948 Triangular Pastures 小结
Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The o ...
- JavaScript 第一课
今天进入到了js的阶段,了解到了JavaScript是一个很重要的阶段,所以要好好的理清每一个知识点 JavaScript的使用: 在<head>标签里应用<script> ...
- JavaScript 之 HelloWorld编写
HelloWorld.html 代码如下: <html><body><script type="text/javascript">documen ...
- linux UART
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <errno.h ...
- Win7 JBOSS的下载安装、环境变量配置以及部署
1. 下载安装 http://jbossas.jboss.org/downloads/ 我下载的是:JBoss AS7.1.1.Final 2. 解压安装包 D:\Java\jboss-as-7.1 ...