APP内发送邮件的功能用的不是特别多,但是有些情况下还是需要的,最近用到了,拿来分享一下。
一、引用头文件库:
#import <MessageUI/MFMailComposeViewController.h>
实现MFMailComposeViewControllerDelegate 的方法。
二、代码实现
- (void)sendByEmail{
MFMailComposeViewController *mailSender = [[MFMailComposeViewController alloc]init];
mailSender.mailComposeDelegate = self;
[mailSender setSubject:@""];
[mailSender setMessageBody:@"" isHTML:NO];
[mailSender setToRecipients:[NSArray arrayWithObjects:@"xxx@163.com", nil]];
[mailSender addAttachmentData:datamimeType:mimeTypefileName:fileName];
[self presentViewController:mailSender animated:YES completion:^{
//
}];
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[controller dismissViewControllerAnimated:YES completion:nil];
switch(result) {
case MFMailComposeResultCancelled:
{
[SVProgressHUD showInfoWithStatus:@"发送取消"];
}
break;
case MFMailComposeResultSaved:
{
[SVProgressHUD showInfoWithStatus:@"存储成功"];
}
break;
case MFMailComposeResultSent:
{
[SVProgressHUD showInfoWithStatus:@"发送成功"];
}
break;
case MFMailComposeResultFailed:
{
[SVProgressHUD showInfoWithStatus:@"发送失败"];
}
break;
}
}