需要导入<MessageUI/MessageUI.h>框架
/* 发送邮件 */
- (IBAction)email:(id)sender {
if (NO == [MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"您还未配置邮箱账户,是否现在跳转配置?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
return;
}
// 如果没有配置邮箱的话,创建出来的对象为nil,弹出会crash
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
// 设置picker的委托方法,完成之后会自动调用成功或失败的方法
picker.mailComposeDelegate = self;
// 添加主题
[picker setSubject:@"案件报告"];
// 添加收件人
NSArray *toRecipients = [NSArray arrayWithObject:@"404401637@163.com"];
// 添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@qq.com",@"third@qq.com", nil];
// 添加密送
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// 拼接邮件body默认显示的信息
NSString *emailBody = [NSString stringWithFormat:@"分享自:腾讯扣扣.诉讼报告.app\n<img src='http://static.588ku.com/imgPath/public/images/ku-logoNewV1.png' /><p>我转载的图片</p>"];
[picker setMessageBody:emailBody isHTML:YES];
// 添加word文档附件
NSString *path = [[NSBundle mainBundle] pathForResource:@"诉讼报告-2018.docx" ofType:nil];
NSData *docData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:docData mimeType:@"application/msword" fileName:@"诉讼报告-2018.docx"];
// 添加图片附件
NSData *myData = UIImageJPEGRepresentation([UIImage imageNamed:@"header_cry_icon"], 1.0);
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"new.png"];
[self presentViewController:picker animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"邮件发送取消"); // 邮件发送取消
break;
case MFMailComposeResultSaved:
NSLog(@"邮件保存成功"); // 邮件保存成功
break;
case MFMailComposeResultSent:
NSLog(@"邮件发送成功"); // 邮件发送成功
break;
case MFMailComposeResultFailed:
NSLog(@"邮件发送失败"); // 邮件发送失败
break;
default:
NSLog(@"邮件未发送");
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
// 跳到邮箱去
NSURL *url = [NSURL URLWithString:@"mailto://"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}
- 其他附件配置
/* 发送图片附件 */
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy.jpg"];
/* 发送txt文本附件 */
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"text/txt" fileName:@"MyText.txt"];
/* 发送doc文本附件 */
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"text/doc" fileName:@"MyText.doc"];
/* 发送pdf文档附件 */
NSString *path = [[NSBundlemainBundle]pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
NSData *myData = [NSDatadataWithContentsOfFile:path];
[pickeraddAttachmentData:myDatamimeType:@"file/pdf"fileName:@"rainy.pdf"];
==配置iOS自带邮件账号注意==
- 如果没有配置邮箱账号直接跳到邮箱会崩溃,所以要处理下
- 账号里面的IMAP、SMTP的密码,不是指邮箱的密码,是第三方邮箱配置授权码,需要到对应的邮箱管理置平台去获取
- 跳转系统邮箱前可以配置一些收件人、抄送人等信息,但是要确保所有收件人的邮箱地址可用,不然会导致这封邮件发不出去,所有人都收不到,这个跟网页上操作不一样,网页上是会全发出去,没收到就没收到