MFMailComposeViewController<发送邮件>

---写在前面---

最近在做发邮件时候,用

//[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"mailto:18506666725@l63.com"]];

这段代码时候有一个问题,就是:在给163邮箱发送的时候不会成功,于是就想换一种方式:如题目,下面会贴代码。

步骤:

1. + canSendMail: 判断该设备是否支持发送邮件

2. 创建MFMailComposeViewController 对象

3.为创建的对象设置

1>(收件人) toRecipients:

2>(抄送/密送)bccRecipients:

3>(主题)subject

4>(内容)setMessageBody:isHTML: 

5>(附件)addAttachmentData:mineType:fileName:

4.为MFMailComposeViewController 设置MFMailComposeViewControllerDelegate,--必须实现的代理方法--- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error


代码:

#import"ViewController.h"

#import

@interfaceViewController()<MFMailComposeViewControllerDelegate>

@end

@implementationViewController

- (IBAction)sendEmailUseMF:(id)sender {

if([MFMailComposeViewController canSendMail]) {// 判断设备是否支持发送邮件

// 创建对象

MFMailComposeViewController* picker = [[MFMailComposeViewControlleralloc]init];

// 设置代理

picker.mailComposeDelegate=self;


picker.navigationBar.tintColor= [UIColorblackColor];

// 设置收件人

[pickersetToRecipients:[NSArrayarrayWithObject:@"18503096725@163.com"]];

在此为了简便没有设置抄送/密送,方法类似

// 设置主题

[pickersetSubject:@"aini"];

// 设置邮件内容

[pickersetMessageBody:@"bieshuohuawenwo"isHTML:NO];

//显示MFMailComposeViewController控制器,点击发送即可

[selfpresentViewController:pickeranimated:YEScompletion:nil];

//}

}

#pragma mark -MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{


switch(result) {//这里可以采用弹窗或HUD提示用户

caseMFMailComposeResultSent:

NSLog(@"发送成功") ;

break;

caseMFMailComposeResultCancelled:

NSLog(@"取消") ;

break;

caseMFMailComposeResultSaved:

NSLog(@"保存") ;

break;

caseMFMailComposeResultFailed:

NSLog(@"发送失败") ;

break;

default:

break;

}

[self dismissViewControllerAnimated:YES completion:nil];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容