IOS中提供了两张方法发送电子邮件,一种方式是使用openURL,另外一种是MFMailComposeViewController。
1、openURL发送电子邮件
使用openURL调用系统邮箱客户端是在IOS3.0以下实现发送邮件的主要手段,我们通过设置url的相关参数来指定邮件的内容,缺点是在发送电子邮件的过程中程序会退出。下面我们来看使用openURL发送电子邮件代码如下:
-(void)BtnSendMaillUseSystem{
NSMutableString *mailUrl=[[[NSMutableString alloc] init] autorelease];
//添加收件人
NSArray *toReceipents=[NSArray arrayWithObjects:@"admin@163.com",nil];
[mailUrl appendFormat:@"mailto:%@",[toReceipents componentsJoinedByString:@","]];
//添加抄送
NSArray *ccRecipients=[NSArray arrayWithObject:@"123456789@qq.com"];
[mailUrl appendFormat:@"?cc=%@",[ccRecipients componentsJoinedByString:@","]];
//添加密送
NSArray *bccRecipients=[NSArray arrayWithObject:@"admin@gmail.com"];
[mailUrl appendFormat:@"&bcc=%@",[bccRecipients componentsJoinedByString:@","]];
//添加主题
[mailUrl appendFormat:@"&subject=my email"];
//添加邮件内容
[mailUrl appendFormat:@"&body=<b>Test Email</b> body!"];
NSString *email=[mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
//也可以如下调用,不设置其他项
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:admin@163.com"]];
}
2、MFMailComposeViewController发送邮件
MFMailComposeViewController是在IOS3.0以后新增的接口,在使用的时候我们需要在framework中添加MessageUI.framework,通过调用MFMailComposeViewController,我们可以把邮件发送窗口集成到我们的app应用中,这样发送邮件的时候就不需要退出应用程序了。
MFMaillComposeViewController的调用:
1、首先在framework中添加,MessageUI.framework
2、引入
源代码下载地址:http://download.csdn.net/detail/u011872945/6862045
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
3、实现MFMailComposeViewControllerDelegate代理,处理邮件发送
具体实现代码如下:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface ViewController : UIViewController
-(IBAction)BtnSendMailInApp;
-(IBAction)BtnSendMaillUseSystem;
@end
#import "ViewController.h"
@interface ViewController ()<MFMailComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)alertWithMessage:(NSString *)message{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:message
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"确定", nil];
[alert show];
}
-(void)BtnSendMailInApp{
Class mailclass=(NSClassFromString(@"MFMailComposeViewController"));
if(mailclass){
if([mailclass canSendMail]){
[self SendEmail];
}else{
[self alertWithMessage:@"用户没有设置邮件账户"];
}
}else{
[self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];
}
}
-(NSString *)fullBundlePathFromRelativePath:(NSString *)relativepath{
NSAssert(relativepath, @"必须指定文件路径",nil);
NSString *filename=[relativepath stringByDeletingPathExtension];
NSString *filetype=[relativepath pathExtension];
return [[NSBundle mainBundle] pathForResource:filename ofType:filetype];
}
-(void)SendEmail{
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate=self;
//设置主题
[mail setSubject:@"测试发送电子邮件"];
//添加收件人
NSArray *toRecipients=[NSArray arrayWithObject:@"admin@163.com"];
[mail setToRecipients:toRecipients];
//添加抄送;
NSArray *ccRecipients=[NSArray arrayWithObjects:@"test1@163.com",@"test@qq.com", nil];
[mail setCcRecipients:ccRecipients];
//添加密送
NSArray *bccRecipients=[NSArray arrayWithObject:@"admin@gmail.com"];
[mail setBccRecipients:bccRecipients];
//添加一张图片
UIImage *addPic=[UIImage imageNamed:@"Icon120.png"];
NSData *imageData=UIImagePNGRepresentation(addPic);
[mail addAttachmentData:imageData mimeType:@"" fileName:@"icon"];
//添加一个xml附件
NSString *file=[self fullBundlePathFromRelativePath:@"model.xml"];
NSData *xmldata=[NSData dataWithContentsOfFile:file];
[mail addAttachmentData:xmldata mimeType:@"" fileName:@"model"];
NSString *emailBody=@"<font color='red'>send test email</font>";
[mail setMessageBody:emailBody isHTML:YES];
[self presentViewController:mail animated:YES completion:nil];
[mail release];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
[self dismissViewControllerAnimated:YES completion:nil];
NSString *messagetip;
switch(result){
case MFMailComposeResultCancelled:
messagetip=@"用户取消了发送电子邮件";
break;
case MFMailComposeResultFailed:
messagetip=@"用户试图保存或者发送电子邮件失败";
break;
case MFMailComposeResultSaved:
messagetip=@"用户成功保存电子邮件";
break;
case MFMailComposeResultSent:
messagetip=@"用户点击发送,将邮件放到发送队列中";
break;
default:
break;
}
[self alertWithMessage:messagetip];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end