iOS发邮件功能

步骤

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服务---

2)在真机中  打开  设置----邮件.通讯录.日历----添加账户---其他----添加邮件账户(填写信息完成后,填写你要发送到的邮箱)---保存
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值