您的位置:首页 > 移动开发 > IOS开发

IOS开发中发送电子邮件的两种方法

2013-11-02 11:47 495 查看
IOS系统框架提供的两种发送电子邮件的方法:openURL和


1.openURL

使用openURL调用系统邮箱客户端是我们在IOS3.0以下实现发邮件功能的主要手段。我们可以通过设置url里的相关参数来指定邮件的内容,不过其缺点很明显,这样的过程会导致程序暂时退出。下面是使用openURL来发邮件的一个小例子:

的#pragma商标
- 使用系统的邮件客户端发送邮件


- (无效)launchMailApp

{

NSMutableString的mailUrl = [[NSMutableString的头]初始化]自动释放];

/
/添加收件人

NSArray的toRecipients = NSArray的arrayWithObject:@ “first@example.com” ];

[mailUrl appendFormat:@ 的“mailto:%@” ,[toRecipients
componentsJoinedByString:@ “,” ];

/
/添加抄送

NSArray的* ccRecipients = [NSArray的arrayWithObjects:@ “second@example.com” ,@“third@example.com” ,零];

[mailUrl appendFormat: “ 闭=%@” ,[ccRecipients
componentsJoinedByString:@ “,” ]];

/
/添加密送

的NSArray * bccRecipients = [NSArray的arrayWithObjects:@ “fourth@example.com的,零];

[mailUrl appendFormat:@ “和体心立方=%@” ,[bccRecipients
componentsJoinedByString:@ “,” ]];

/
/添加主题

[mailUrl使用AppendString:@ “&主题=我的电子邮件” ];

/
/添加邮件内容

@ [mailUrl使用AppendString:=
<B>电子邮件</ b>的身体!“ ];

的NSString *电子邮件= [mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[时候UIApplication sharedApplication]的OpenURL:[NSURL URLWithString:电子邮件];

}


2.MFMailComposeViewController

MFMailComposeViewController是在IOS3.0新增的一个接口,它在MessageUI.framework中。通过调用MFMailComposeViewController,可以把邮件发送窗口集成到我们的应用里,发送邮件就不需要退出程序了。MFMailComposeViewController的使用方法:

1,项目中引入MessageUI.framework;

2,在使用的文件中导入MFMailComposeViewController.h头文件;

3实现MFMailComposeViewControllerDelegate,处理邮件发送事件;

4,调出邮件发送窗口前先使用MFMailComposeViewController里的“+(BOOL)canSendMail”方法检查用户是否设置了邮件账户;

5初始化MFMailComposeViewController,构造邮件体

/
/

/ / ViewController.h的

/ / MailDemo的

/ /

/ /创建者LUOYL 12-4-4。

/
/版权所有(c)2012年http://luoyl.info。保留所有权利。

/ /

#导入<UIKit/UIKit.h>“

#导入<MessageUI/MFMailComposeViewController.h>

@接口的ViewController:的UIViewController <MFMailComposeViewControllerDelegate>的

@结束

的#pragma标志
- 在应用内发送邮件


/ /激活邮件功能

- (无效)sendMailInApp

{

类mailClass =(NSClassFromString(@ “MFMailComposeViewController” ));

如果 (mailClass){

[自我

返回;

}

(![mailClass
canSendMail]){

[个体经营alertWithMessage: “ 用户没有设置邮件账户” ;

返回;

}

[自我displayMailPicker;

}

/ /调出邮件发送窗口

- (无效)displayMailPicker

{

MFMailComposeViewController mailPicker = [MFMailComposeViewController ALLOC]的init];

mailPicker.mailComposeDelegate =自我;

/
/设置主题

[mailPicker SETSUBJECT: “ 电子邮件主题” ];

/
/添加收件人

NSArray的toRecipients = NSArray的arrayWithObject:@ “first@example.com” ];

[mailPicker setToRecipients:的toRecipients];

/
/添加抄送

NSArray的* ccRecipients = [NSArray的arrayWithObjects:@ “second@example.com” ,@“third@example.com” ,零];

[mailPicker setCcRecipients:的ccRecipients];

/
/添加密送

的NSArray * bccRecipients = [NSArray的arrayWithObjects:@ “fourth@example.com的,零];

[mailPicker setBccRecipients:的bccRecipients];

/
/添加一张图片

的UIImage * addPic = [UIImage的imageNamed:@ “Icon@2x.png” ];

NSData的* imageData里= UIImagePNGRepresentation(addPic); /
/ PNG

/
/关于mime类型:http://www.iana.org/assignments/media-types/index.html

[mailPicker addAttachmentData:imageData里mime类型:@ “ 文件名
​​:@ “的icon.png” ];

/
/添加一个PDF附件

的NSString *文件= [个体经营fullBundlePathFromRelativePath:@ “高质量C
+ +编程指南。” ];

NSData的PDF = [NSData的dataWithContentsOfFile:文件];

[mailPicker addAttachmentData:PDF mime类型:@ “ 文件名
​​:“高质量C + +编程指南。” ];

的NSString * emailBody = @ “我color='red'>电子邮件</
FONT>正文” ;

[mailPicker setMessageBody:emailBody isHTML:YES];

[自我presentModalViewController:动画mailPicker:是的];

[mailPicker释放];

}

的#pragma标志 - 实现MFMailComposeViewControllerDelegate

- (无效)mailComposeController:(MFMailComposeViewController
*)控制器didFinishWithResult:(MFMailComposeResult)结果的误差:(NSError *)错误

{

/
/关闭邮件发送窗口

[自dismissModalViewControllerAnimated:YES];

的NSString *味精;

开关 (结果){

情况 MFMailComposeResultCancelled:

味精= @ “用户取消编辑邮件” ;

打破;

情况 MFMailComposeResultSaved:

味精= @ “用户成功保存邮件” ;

打破;

情况 MFMailComposeResultSent:

味精= @ “用户点击发送,将邮件放到队列中,还没发送” ;

打破;

情况 MFMailComposeResultFailed:

味精= @ “用户试图保存或者发送邮件失败” ;

打破;

默认情况下

味精= @ “ ;

打破;

}

[自alertWithMessage:味精];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: