根据http://luoyl.info/blog/2012/04/two-way-to-send-email-in-ios/ 总结
1,首先加载 MessageUI控件 ,在.h文件中导入,并且引入协议
#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
//导入控件
@interface AboutViewController : UIViewController<MFMailComposeViewControllerDelegate>//引入这个
- (IBAction)email:(id)sender;
@end
2,在.m文件中声明所需的两个方法
#import "AboutViewController.h"
@interface AboutViewController ()
- (void)displayMailPicker;////调出邮件发送窗口
- (void)alertWithMessage:(NSString*)message;//提示消息
@end
3,写好方法
#pragma mark - 在应用内发送邮件
//激活邮件功能
- (void)sendMailInApp
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (!mailClass) {
[self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];
return;
}
if (![mailClass canSendMail]) {
[self alertWithMessage:@"用户没有设置邮件账户"];
return;
}
[self displayMailPicker];
}
//调出邮件发送窗口
- (void)displayMailPicker
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
//设置主题
[mailPicker setSubject: @"冷笑话反馈"];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"lengxiaohua@me.com"];
[mailPicker setToRecipients: toRecipients];
// //添加抄送
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// [mailPicker setCcRecipients:ccRecipients];
// //添加密送
// NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
// [mailPicker setBccRecipients:bccRecipients];
// // 添加一张图片
// UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
// NSData *imageData = UIImagePNGRepresentation(addPic); // png
// //关于mimeType:http://www.iana.org/assignments/media-types/index.html
// [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
//
// //添加一个pdf附件
// NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
// NSData *pdf = [NSData dataWithContentsOfFile:file];
// [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
//
NSString *emailBody = @"<font color='red'>内容:</font> ";
[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController: mailPicker animated:YES];
[mailPicker release];
}
#pragma mark - 实现 MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
//关闭邮件发送窗口
[self dismissModalViewControllerAnimated:YES];
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
msg = @"用户取消编辑邮件";
break;
case MFMailComposeResultSaved:
msg = @"用户成功保存邮件";
break;
case MFMailComposeResultSent:
msg = @"用户点击发送,将邮件放到队列中,还没发送";
break;
case MFMailComposeResultFailed:
msg = @"用户试图保存或者发送邮件失败";
break;
default:
msg = @"";
break;
}
[self alertWithMessage:msg];
}
- (void)alertWithMessage:(NSString*)message
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
;
;
}
4,在所需要弹出邮件层的按钮添加上述方法
- (IBAction)email:(id)sender {
[self sendMailInApp];
}
整个.m文件的内容如下:
//
// AboutViewController.m
// Created by xxx
//
//
#import "AboutViewController.h"
@interface AboutViewController ()
- (void)displayMailPicker;////调出邮件发送窗口
- (void)alertWithMessage:(NSString*)message;//提示消息
@end
@implementation AboutViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)email:(id)sender {
[self sendMailInApp];
}
#pragma mark - 在应用内发送邮件
//激活邮件功能
- (void)sendMailInApp
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (!mailClass) {
[self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];
return;
}
if (![mailClass canSendMail]) {
[self alertWithMessage:@"用户没有设置邮件账户"];
return;
}
[self displayMailPicker];
}
//调出邮件发送窗口
- (void)displayMailPicker
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
//设置主题
[mailPicker setSubject: @"冷笑话反馈"];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObject: @"lengxiaohua@me.com"];
[mailPicker setToRecipients: toRecipients];
// //添加抄送
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// [mailPicker setCcRecipients:ccRecipients];
// //添加密送
// NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
// [mailPicker setBccRecipients:bccRecipients];
// // 添加一张图片
// UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];
// NSData *imageData = UIImagePNGRepresentation(addPic); // png
// //关于mimeType:http://www.iana.org/assignments/media-types/index.html
// [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
//
// //添加一个pdf附件
// NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
// NSData *pdf = [NSData dataWithContentsOfFile:file];
// [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
//
NSString *emailBody = @"<font color='red'>内容:</font> ";
[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController: mailPicker animated:YES];
[mailPicker release];
}
#pragma mark - 实现 MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
//关闭邮件发送窗口
[self dismissModalViewControllerAnimated:YES];
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
msg = @"用户取消编辑邮件";
break;
case MFMailComposeResultSaved:
msg = @"用户成功保存邮件";
break;
case MFMailComposeResultSent:
msg = @"用户点击发送,将邮件放到队列中,还没发送";
break;
case MFMailComposeResultFailed:
msg = @"用户试图保存或者发送邮件失败";
break;
default:
msg = @"";
break;
}
[self alertWithMessage:msg];
}
- (void)alertWithMessage:(NSString*)message
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
;
;
}
- (void)dealloc {
[super dealloc];
}
@end
原文链接: https://www.cnblogs.com/lostinthenslog/archive/2012/09/20/2695539.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/63316
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!