使用MFMailComposeViewController
发送邮件。
1、导入头文件
#import <MessageUI/MessageUI.h>
2、实现MFMailComposeViewControllerDelegate
代理方法
3、示例
// 判断用户是否已设置邮件账户
if ([MFMailComposeViewController canSendMail]) {
// 弹出邮件发送视图
MFMailComposeViewController *emailVC = [[MFMailComposeViewController alloc] init];
// 设置邮件代理
[emailVC setMailComposeDelegate:self];
// 设置收件人
[emailVC setToRecipients:@[kEmail]];
// 设置抄送人
// [_emailVC setCcRecipients:@[@"1622849369@qq.com"]];
// 设置密送人
// [_emailVC setBccRecipients:@[@"15690725786@163.com"]];
// 设置邮件主题
[emailVC setSubject:@"意见反馈【二维码识别】"];
//设置邮件的正文内容
NSString *emailContent = @"邮件内容: ";
// 是否为HTML格式
[emailVC setMessageBody:emailContent isHTML:NO];
// 如使用HTML格式,则为以下代码
// [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World!</p></body></html>" isHTML:YES];
// 添加附件
// UIImage *image = [UIImage imageNamed:@"qq"];
// NSData *imageData = UIImagePNGRepresentation(image);
// [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"qq.png"];
// NSString *file = [[NSBundle mainBundle] pathForResource:@"EmptyPDF" ofType:@"pdf"];
// NSData *pdf = [NSData dataWithContentsOfFile:file];
// [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"EmptyPDF.pdf"];
[self presentViewController:emailVC animated:YES completion:nil];
} else {
// 给出提示,设备未开启邮件服务
InsertAlert(UIAlertViewStyleDefault, @"没有邮件帐户", @"请添加邮件帐户(添加方法:设置->邮件、通讯录、日历->添加帐户->其他->添加邮件帐户)", 0, nil, nil, kAlertRealized);
}
- (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;
}
// 关闭邮件发送视图
[self dismissViewControllerAnimated:YES completion:nil];
}
网友评论