1.引入MessageUI.framework框架
2.实例化类MFMailComposeViewController,判断用户的设备是否设置邮箱 if ([mailClass canSendMail])
3.具体代码:
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; //创建邮件controller
mailPicker.mailComposeDelegate = self; //设置邮件代理
[mailPicker setSubject:@"Send WebView ScreenShot"]; //邮件主题
[mailPicker setToRecipients:[NSArray arrayWithObjects:@"lipengxuan48@163.com",
@"lipengxuan48@gmail.com", nil]]; //设置发送给谁,参数是NSarray
//cc
// [mailPicker setCcRecipients:[NSArray arrayWithObject:@"zhuqil@163.com"]]; //可以添加抄送
//bcc
// [mailPicker setBccRecipients:[NSArray arrayWithObject:@"secret@gmail.com"]];
[mailPicker setMessageBody:@"WebShotScreen n in Attachment!" isHTML:NO]; //邮件主题
NSData *imageData = UIImagePNGRepresentation(viewImage);//这里获取截图存入NSData,用于发送附件
[mailPicker addAttachmentData:imageData mimeType:@"image/png" fileName:@"WebScreenShot"];//发送附件的NSData,类型,附件名
[self presentModalViewController:mailPicker animated:YES]; //把当前controller变为邮件controller
4.实现代理方法
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;
default: break;
}
[self dismissModalViewControllerAnimated:YES];
}