if (mailController == nil) {
mailController = [[MFMailComposeViewController alloc]init];
mailController.mailComposeDelegate = self;}
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
3.判断当前设备是否支持邮件发送:[MFMailComposeViewController canSendMail]
-(void)sendMailWithEnclosure:(UIButton*)sender
{
UIButton *btn = sender;
NSString *name = nil;
NSString *mimeType = nil;
if (btn.tag == 10) {
name =@"pdf附件.pdf";
mimeType =@"application/pdf";
}else if(btn.tag == 20){
name =@"pages附件.doc";
mimeType =@"application/msword";
}
if ([MFMailComposeViewControllercanSendMail]) {
[mailControllersetSubject:@"测试邮件发送附件"];
[mailControllersetToRecipients:[NSArrayarrayWithObjects:@"ouyang****@163.com", nil]];
[mailControllersetMessageBody:@"Test SendMailWithEnclosure!"isHTML:NO];
NSString *path = [[NSBundlemainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:name];
NSData *Data = [NSDatadataWithContentsOfFile:finalPath];
//发送文件的NSData,类型,附件名
[mailController addAttachmentData:Data mimeType:mimeType fileName:name];
NSLog(@"%@,%@",mimeType,name);
[self presentViewController:mailController animated:YES completion:nil];
//把当前controller变为mailcontroller
}
}
5.代理方法中对邮件发送结果进行判断:
#pragma mark -MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
NSString *resultString = nil;
switch (result){
caseMFMailComposeResultCancelled:
resultString = [NSString stringWithFormat:@"Mail send canceled…"];
break;
caseMFMailComposeResultSaved:
resultString = [NSString stringWithFormat:@"Mail saved…"];
break;
caseMFMailComposeResultSent:
resultString = [NSString stringWithFormat:@"Mail sent out…"];
break;
caseMFMailComposeResultFailed:
resultString = [NSStringstringWithFormat:@"%@", [error localizedDescription]];
break;
default:
break;
}
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:resultString delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:nil];
mailController = nil;
}
mailto四个常用的参数
subject -- 代表邮件的标题
body -- 代表邮件的内容
cc -- 代表一个抄送对象
bcc -- 代表一个暗送对象
NSString*urlString =@"mailto:abc@example.com?cc=cde@example.com&subject=Greetings!&body=Wishes!";
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];