APP內(nèi)發(fā)送郵件的功能用的不是特別多延欠,但是有些情況下還是需要的,最近用到了,拿來分享一下愧捕。
一、引用頭文件庫:
#import <MessageUI/MFMailComposeViewController.h>
實(shí)現(xiàn)MFMailComposeViewControllerDelegate 的方法申钩。
二次绘、代碼實(shí)現(xiàn)
- (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:@"發(fā)送取消"];
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case MFMailComposeResultSaved:
? ? ? ? {
? ? ? ? ? ? [SVProgressHUD showInfoWithStatus:@"存儲成功"];
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case MFMailComposeResultSent:
? ? ? ? {
? ? ? ? ? ? [SVProgressHUD showInfoWithStatus:@"發(fā)送成功"];
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case MFMailComposeResultFailed:
? ? ? ? {
? ? ? ? ? ? ?[SVProgressHUD showInfoWithStatus:@"發(fā)送失敗"];
? ? ? ? }
? ? ? ? ? ? break;
? ? }
}