方法一:
•用自带的邮件客户端,发完邮件后不会自动回到原应用
//1、设置发送mail的URL NSURL *url = [NSURL URLWithString:@"mailto://10010@qq.com"]; //2、由于是应用级别的操作,需要使用UIApplication [[UIApplication sharedApplication] openURL:url];
方式二:
•如果想指定邮件内容,那就得使用MessageUI框架
•包含主头文件:#import <MessageUI/MessageUI.h>
•实现代理:<MFMailComposeViewControllerDelegate>
•主要代码
// 1、实例化发送邮件的控制器 MFMailComposeViewController *vc = [[MFMailComposeViewController alloc]init]; // 2、设置的主题 [vc setSubject:@"coco"]; // 3、邮件的内容 [vc setMessageBody:@"coco今天下午吃hi啥" isHTML:NO]; // 4、收件人 [vc setToRecipients:@[@"604558817@qq.com"]]; // 5、抄送 [vc setCcRecipients:@[@"1234@qqq.com"]]; // 6、密送 [vc setBccRecipients:@[@"777@qq.com"]]; // 7、设置控制器为代理 vc.mailComposeDelegate = self; // 8、使用模态的形式展示控制器 [self presentViewController:vc animated:YES completion:nil];
•实现代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { // result返回值是一个枚举类型,判断进行相应的操作 if (MFMailComposeResultCancelled == result) { NSLog(@"取消发送"); }else if (MFMailComposeResultSaved == result) { NSLog(@"保存"); }else if (MFMailComposeResultSent == result) { NSLog(@"发送成功"); }else if (MFMailComposeResultFailed == result) { NSLog(@"发送失败"); } // 不管结果如何,关闭当前的控制器 [self dismissViewControllerAnimated:YES completion:^{ }]; }
相关效果图展示: