IOS 发送Email的方法 兼容iPad iPhone

本文介绍了一个iOS应用如何使用MFMailComposeViewController来发送带有图片和PDF附件的电子邮件,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IOS 发送Email的方法 兼容iPad iPhone


//调出邮件发送窗口
- (void)displayMailPicker
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;

//设置主题
[mailPicker setSubject: @"图片分享"];
//添加收件人
// NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];
// [mailPicker setToRecipients: toRecipients];
//添加抄送
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// [mailPicker setCcRecipients:ccRecipients];
//添加密送
// NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];
// [mailPicker setBccRecipients:bccRecipients];

NSArray *tempExport = self.selectedTag == SelectedAllTag ? self.fileInfoArray : self.selectedFileInfoArray;
for (FileInfo *fileInfo in tempExport) {
// 添加一张图片
UIImage *addPic = [UIImage imageWithContentsOfFile:fileInfo.path];
NSData *imageData = UIImagePNGRepresentation(addPic); // png
//关于mimeType:http://www.iana.org/assignments/media-types/index.html
[mailPicker addAttachmentData: imageData mimeType: @"" fileName: fileInfo.name];
}


//添加一个pdf附件
// NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
// NSData *pdf = [NSData dataWithContentsOfFile:file];
// [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];

// NSString *emailBody = @"<font color='red'>eMail</font> 正文";
// [mailPicker setMessageBody:emailBody isHTML:YES];
//[self.navigationController pushViewController:mailPicker animated:YES];
[self presentViewController:mailPicker animated:YES completion:^{
[HUDUtil removeHUD];
}];
}


/**
* 邮件代理
*
* @param controller <#controller description#>
* @param result <#result description#>
* @param error <#error description#>
*/
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0)
{
//关闭邮件发送窗口
[self dismissViewControllerAnimated:YES completion:^{
[self.navigationController popViewControllerAnimated:NO];
}];
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
msg = @"用户取消编辑邮件";
break;
case MFMailComposeResultSaved:
msg = @"用户成功保存邮件";
break;
case MFMailComposeResultSent:
msg = @"用户点击发送,将邮件放到队列中,还没发送";
break;
case MFMailComposeResultFailed:
msg = @"用户试图保存或者发送邮件失败";
break;
default:
msg = @"";
break;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值