步骤
1.先导入MessageUI.framework系统库
2.#import <MessageUI/MFMailComposeViewController.h>
3.签代理MFMailComposeViewControllerDelegate
4.代码
- (void)businessContactWithMail
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
//判断能否发送邮件
if ([mailClass canSendMail])
{
//创建邮件控制器,准备发送邮件
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
//设置主题
[controller setSubject:@"主题"];
//主收件人
[controller setToRecipients:@[@"<span style="font-family: SimSun;">邮箱网址</span>"]];
//抄送
[controller setCcRecipients:@[@"<span style="font-family: SimSun;">邮箱网址</span>"]];
//秘密抄送
[controller setBccRecipients:@[@"<span style="font-family: SimSun;">邮箱网址</span>"]];
//正文
[controller setMessageBody:@"正文" isHTML:NO];
//设置代理
[controller setMailComposeDelegate:self];
//显示控制器
[self presentViewController:controller animated:YES completion:^{
}];
[controller release];
}
else
{
NSLog(@"您的设备尚未配置邮件账号");
}
} else {
NSLog(@"您的设备不支持邮件功能");
}
}
//发完邮件调用代理方法关闭窗口
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
/**
MFMailComposeResultCancelled, 取消
MFMailComposeResultSaved, 保存邮件
MFMailComposeResultSent, 已经发送
MFMailComposeResultFailed 发送失败
*/
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 dismissViewControllerAnimated:YES completion:nil];
}
5.在真机测试时控制台会打印出 您的设备尚未配置邮件账号
解决办法
1)打开邮箱(以qq邮箱为例) 设置----账户----POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务---
POP3/SMTP服务 (
如何使用 Foxmail 等软件收发邮件?)
已开启 |
关闭
IMAP/SMTP服务 (
什么是 IMAP,它又是如何设置?)
已开启 |
关闭
CardDAV/CalDAV服务 (
什么是CardDAV/CalDAV,它又是如何设置?)
2)在真机中 打开 设置----邮件.通讯录.日历----添加账户---其他----添加邮件账户(填写信息完成后,填写你要发送到的邮箱)---保存