---写在前面---
最近在做发邮件时候,用
//[[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];
}