iOS调用系统功能发邮件

使用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:@[@"[email protected]"]];
  // 设置抄送人
  [mailCompose setCcRecipients:@[@"[email protected]"]];
  // 设置密抄送
  [mailCompose setBccRecipients:@[@"[email protected]"]];
  /**
   *  设置邮件的正文内容
   */
  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图片数据

时间: 2024-08-07 08:30:54

iOS调用系统功能发邮件的相关文章

IOS问题汇总:2015-1-9 iOS 调用系统发短信以及打电话功能

iOS 调用系统发短信以及打电话功能 ios电话smsinterface互联网class先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@“tel://10086”]]; 调用发短信功能 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@“sms://10000”]]; 上面的发短信的功能是调用系统的

IOS 调用系统发短信以及打电话的功能

IOS 调用系统发短信以及打电话的功能 http://blog.csdn.net/lwq421336220/article/details/7818979 先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调用发短信功能 [[UIApplication sharedApplication]openURL:[NSURL URLWithS

iOS调用系统发短信功能详解

iOS调用系统的发短信功能可以分为两种:1,程序外调用系统发短信.2,程序内调用系统发短信.第二种的好处是用户发短信之后还可以回到app.这对app来说非常重要. 程序外调用系统发短信 这个方法其实很简单,直接调用openURL即可: [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://13888888888"]]; 程序内调用系统发短信 1)导入MessageUI.framework,并引入头文

ios 调用打电话,发短信的功能

首先: Frameworks中要引入MessageUI.framework #import <MessageUI/MessageUI.h> 添加协议:<MFMessageComposeViewControllerDelegate> 之后看代码: 有两种短信调用,1种是调用系统内的短信功能,可以发完短信返回app,另一种调用系统外的发短信功能,不能反悔app //调用系统内的发短信功能,可以返回app if ([MFMessageComposeViewController canSe

调用打电话发短信

iOS 调用系统发短信以及打电话功能 分类: iOS 短信 电话 2012-08-01 16:29 13092人阅读 评论(0) 收藏 举报 ios电话smsinterface互联网class 先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调用发短信功能 [[UIApplication sharedApplication]open

iOS 拨打电话 发短信(sms) 发邮件(mail) 调用自带的浏览器 safari

注意 : 以下需要苹果手机测试(真机测试)           一.拨打电话的两种方式 //拨打电话方式1  (弹出提示是否呼叫,推荐这种) UIWebView*callWebview =[[UIWebView alloc] init]; NSURL *telURL =[NSURL URLWithString:@"tel:10086"]; [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]]; //记得添加到vie

iOS应用调用系统打电话、发短信和发邮件功能

摘要: 在应用程序内,调用系统的功能来实现打电话.发短信和发邮件,通过电话号码或者邮箱,直接跳转到系统的功能界面. PS:调试好像只能真机调试,模拟器没有反应,真机就可以跳转,不知道是不是必须真机,但方法肯定是可行的. 1.打电话 应用内调用系统打电话有两种方式: 1)WebView方式 使用WebView来跳转,把电话号码通过URL传递给WebView,这种方式会弹出是否要拨打的提示,可以选择拨打或者不拨打,打完也会自动回到应用界面,推荐. UIWebView *callWebview =[[

iOS中打电话、打开网址、发邮件、发短信等

常用小功能 小功能简介 iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信等 打电话-方法1 最简单最直接的方式:直接跳到拨号界面 NSURL *url = [NSURL URLWithString:@"tel://10010"]; [[UIApplication sharedApplication] openURL:url]; 缺点 电话打完后,不会自动回到原应用,直接停留在通话记录界面 打电话-方法2 拨号之前会弹框询问用户是否拨号,拨完后能

iOS打电话、发短信、发邮件功能

以下为学习过程中在网上查到的方法, 实现打电话的功能,主要二种方法,下面我就分别说说它们的优缺点. 1.1.发短信(1)——URL // 直接拨号,拨号完成后会停留在通话记录中 1.方法: NSURL *url = [NSURL URLWithString:@"sms://10010"]; [[UIApplication sharedApplication] openURL:url]; 2.优点: –简单 3.缺点: –不能指定短信内容,而且不能自动回到原应用 1.2发短信(2)——M