通过 MFMailComposeViewController 发送邮件,需预先登录邮箱账号的情况下;
具体实现与配置参数请参考如下:
首先,引入 MFMailComposeViewController 库
#import <MessageUI/MessageUI.h>
其次,实现相关 api 方法
if ([MFMailComposeViewController canSendMail]) {MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];mailCompose.mailComposeDelegate = self;mailCompose.navigationBar.tintColor = [UIColor blackColor];// 收件人[mailCompose setToRecipients:[NSArray arrayWithObject:@"xxxxxx@163.com"]];// 抄送人[mailCompose setCcRecipients:[NSArray arrayWithObject:@"xxxxxx@163.com"]];// 密抄送人[mailCompose setBccRecipients:[NSArray arrayWithObject:@"xxxxxx@163.com"]];// 主题[mailCompose setSubject:@"标题"];// 内容[mailCompose setMessageBody:@"内容" isHTML:NO];// 附件[mailCompose addAttachmentData:UIImagePNGRepresentation([UIImage imageNamed:@"vipLogo"]) mimeType:@"" fileName:@"icon.png"];[self presentViewController:mailCompose animated:YES completion:nil];} else {// 请先登录邮箱账号}
最后,实现 MFMailComposeViewControllerDelegate 代理方法即可
注:如下代理方法一定要实现!
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {switch (result) {case MFMailComposeResultSent:NSLog(@"[邮件] - 发送成功");break;case MFMailComposeResultCancelled:NSLog(@"[邮件] - 发送取消");break;case MFMailComposeResultSaved:NSLog(@"[邮件] - 保存");break;case MFMailComposeResultFailed:NSLog(@"[邮件] - 发送失败");break;default:break;}// 关闭邮件视图组件[self dismissViewControllerAnimated:YES completion:nil];
}
以上便是此次分享的全部内容,希望能对大家有所帮助!