代码的思路很清晰 比较简单
#import "ViewController.h"
//导入
#import <MessageUI/MessageUI.h>
@interface ViewController ()<MFMailComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//发邮件
#if 0
//方法一:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:1143344@163.com"]];
#endif
//方法二:
//判断当前设置是否支持发送邮件
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
//添加收件人
[mailVC setToRecipients:@[@"1148828736@qq.com",@"test@163.com"]];
//添加抄送人
[mailVC setCcRecipients:@[@"1111@163.com"]];
//添加"密送人"
[mailVC setBccRecipients:@[@"22222@163.com"]];
//添加主题
[mailVC setSubject:@"百度招聘"];
//添加邮件内容
// [mailVC setMessageBody:@"<h1>该还钱了</h1><br><h4>该还钱了</h4>" isHTML:YES];
[mailVC setMessageBody:@"哈哈哈哈哈哈" isHTML:NO];
//添加附件
[mailVC addAttachmentData:UIImagePNGRepresentation([UIImage imageNamed:@"Transfer_Refunded"]) mimeType:@"image/png" fileName:@"test.png"];
//设置代理
mailVC.mailComposeDelegate = self;
[self presentViewController:mailVC animated:YES completion:nil];
}
else
{
NSLog(@"不支持发送邮件");
}
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
/*
MFMailComposeResultCancelled, 取消发送
MFMailComposeResultSaved, 保存
MFMailComposeResultSent, 发送成功
MFMailComposeResultFailed 发送失败
*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end