您的位置:首页 > 移动开发 > IOS开发

iOS发邮件功能

2016-02-29 09:54 501 查看
步骤

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)在真机中 打开 设置----邮件.通讯录.日历----添加账户---其他----添加邮件账户(填写信息完成后,填写你要发送到的邮箱)---保存
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: