iOS调用系统功能发邮件

时间:2021-11-10 03:43:02

使用MFMailComposeViewController发送邮件

1.项目需要导入框架:MessageUI.framework

2.使用的Controller的.h文件中添加代理 MFMailComposeViewControllerDelegate 并且导入头文件:#import <MessageUI/MessageUI.h>

3.判断用户是否设置了邮箱账户

BOOL canSend = [MFMailComposeViewController canSendMail];
if (canSend) {
[self sendEmailAction];
}else{
//提示用户添加邮件账户
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Please Add Your Mail Account:" message:@"Settings->Mail,Contacts,Calendars->Add Account" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:[kGlobal getStringValueWithKey:@"OK"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}

4.sendEmailAction方法代码

- (void)sendEmailAction
{
// 邮件服务器
MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
// 设置邮件代理
[mailCompose setMailComposeDelegate:self];
// 设置邮件主题
[mailCompose setSubject:@"我是邮件主题"];
// 设置收件人
[mailCompose setToRecipients:@[@"1234567890@qq.com"]];
// 设置抄送人
[mailCompose setCcRecipients:@[@"1234567890@qq.com"]];
// 设置密抄送
[mailCompose setBccRecipients:@[@"1234567890@126.com"]];
/**
* 设置邮件的正文内容
*/
NSString *emailContent = @"我是邮件内容";
// 是否为HTML格式
[mailCompose setMessageBody:emailContent isHTML:NO];
// 如使用HTML格式,则为以下代码
/**
NSData *imageData = UIImageJPEGRepresentation(self.cellImage, 0.5);
NSString *encodedString = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]; NSString *emailContent = [NSString stringWithFormat:@"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"></head><body><img src=\"data:image/jpeg;base64,%@\" /><p><table width=\"300px\"><tr><td colspan=\"2\" align=\"left\" style=\"color:red;font-style:italic\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td colspan=\"2\" align=\"left\" style=\"color:red;font-style:italic\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr></table></p></body></html>",encodedString,firstTitle,[kGlobal getStringValueWithKey:@"casePrice"],[Global getFormatStr:self.offering.case_price],[kGlobal getStringValueWithKey:@"casesneeded"],[Global getFormatStr:self.offering.cases_needed],[kGlobal getStringValueWithKey:@"productsavingcases"],[NSString stringWithFormat:@"$%@",[Global getFormatStr:self.offering.product_savings]],[kGlobal getStringValueWithKey:@"annualSaving"],[NSString stringWithFormat:@"$%@",[Global getFormatStr:self.offering.annual_savings]],secondTitle,[kGlobal getStringValueWithKey:@"pricepaidpercase"],[Global getFormatStr:self.operatorVolume.pricePerCase],[kGlobal getStringValueWithKey:@"casespurchasedannually"],[Global getFormatStr:self.competitorProduct.cases_purchase_annuaully],[kGlobal getStringValueWithKey:@"costofcompetitiveproduct"],[Global getFormatStr:self.competitorProduct.cost_of_competitive_product]];//直接把图片转成Base64编码放到邮件里面,注意Base64编码图片的格式(PNG,JPEG)要和压缩的格式一样,否则不能显示
*/
//添加附件
//添加图片附件
UIImage *image = [UIImage imageNamed:@"image"];
NSData *imageData = UIImagePNGRepresentation(image);
[mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"custom.png"];
//添加PDF附件
NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
NSData *pdf = [NSData dataWithContentsOfFile:file];
[mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"test"]; // 弹出邮件发送视图
[self presentViewController:mailCompose animated:YES completion:nil];
}

5.MFMailComposeViewControllerDelegate的代理方法:

- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled: // 用户取消编辑
NSLog(@"Mail send canceled...");
break;
case MFMailComposeResultSaved: // 用户保存邮件
NSLog(@"Mail saved...");
break;
case MFMailComposeResultSent: // 用户点击发送
NSLog(@"Mail sent...");
break;
case MFMailComposeResultFailed: // 用户尝试保存或发送邮件失败
NSLog(@"Mail send errored: %@...", [error localizedDescription]);
break;
}
// 关闭邮件发送视图
[self dismissViewControllerAnimated:YES completion:nil];
}

需要注意的事项:

1.邮件的发送必须真机测试,ipad或iPhone测试都行,否则会崩溃

2.添加的附件在邮件编辑的过程中会直接显示在正文内容后面,发送之后需要下载

3.发送非HTML内容时,正文内容是一个NSString类型的字符串;如果发送HTML类型的数据,需要注意尽量写标准的HTML格式,不然邮件无法解析,我上面写的发送的HTML格式的内容中,实际上混合了一张图片和一个表格,图片采用的是Base64的编码格式,浏览器可以直接解析Base64编码,但是注意邮件不是浏览器,邮件对HTML的格式要求更加严格。我就因为加载JPEG格式的图片时,在HTML中使用了PNG格式的Base64的解码方式,结果在浏览器中可以正常显示,邮件中无法显示。而且通过Base64嵌入到网页中的图片有大小限制,尽量嵌入小图片。

Data URI scheme支持的类型有:
data:,文本数据
data:text/plain,文本数据
data:text/html,HTML代码
data:text/html;base64,base64编码的HTML代码
data:text/css,CSS代码
data:text/css;base64,base64编码的CSS代码
data:text/javascript,Javascript代码
data:text/javascript;base64,base64编码的Javascript代码
data:image/gif;base64,base64编码的gif图片数据
data:image/png;base64,base64编码的png图片数据
data:image/jpeg;base64,base64编码的jpeg图片数据
data:image/x-icon;base64,base64编码的icon图片数据