// MARK: - Action

// MARK: compose mail 发送邮件

@IBAction func composeMail(sender: AnyObject) {

// 判断能否发送邮件

guard MFMailComposeViewController.canSendMail() else {

print("不能发送邮件")

return

}

let mailVC = MFMailComposeViewController()

mailVC.mailComposeDelegate = self // 代理

mailVC.setSubject("阳君") // 主题

mailVC.setToRecipients(["937447974@qq.com"]) // 收件人

mailVC.setCcRecipients(["CcRecipients@qq.com"]) // 抄送

mailVC.setBccRecipients(["bccRecipients@qq.com"]) // 密送

mailVC.setMessageBody("相关内容", isHTML: false) // 内容,允许使用html内容

if let image = UIImage(named: "qq") {

if let data = UIImagePNGRepresentation(image) {

// 添加文件

mailVC.addAttachmentData(data, mimeType: "image/png", fileName: "qq")

}

}

self.presentViewController(mailVC, animated: true, completion: nil)

}

// MARK: compose message 发送短信

@IBAction func composeMessage(sender: AnyObject) {

guard MFMessageComposeViewController.canSendText() else {

print("不能发送短信")

return

}

let messageVC = MFMessageComposeViewController()

messageVC.messageComposeDelegate = self // 代理

messageVC.recipients = ["18511056826"] // 收件人

messageVC.body = "短信内容" // 内容

// 发送主题

if MFMessageComposeViewController.canSendSubject() {

messageVC.subject = "阳君"

}

// 发送附件

if MFMessageComposeViewController.canSendAttachments() {

// 路径添加

if let path = NSBundle.mainBundle().pathForResource("Info", ofType: "plist") {

messageVC.addAttachmentURL(NSURL(fileURLWithPath: path), withAlternateFilename: "Info.plist")

}

// NSData添加

if MFMessageComposeViewController.isSupportedAttachmentUTI("public.png") {

// See [Uniform Type Identifiers Reference](https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Introduction/Introduction.html)

if let image = UIImage(named: "qq") {

if let data = UIImagePNGRepresentation(image) {

// 添加文件

messageVC.addAttachmentData(data, typeIdentifier: "public.png", filename: "qq.png")

}

}

}

}

// messageVC.disableUserAttachments() // 禁用添加附件按钮

self.presentViewController(messageVC, animated: true, completion: nil)

}

// MARK: -  MFMailComposeViewControllerDelegate

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

// 关闭MFMailComposeViewController

controller.dismissViewControllerAnimated(true, completion: nil)

guard error == nil else { // 错误拦截

print(error)

return

}

switch result { // 发送状态

case MFMailComposeResultCancelled:

print("Result: Mail sending canceled") // 删除草稿

case MFMailComposeResultSaved: // 存储草稿

print("Result: Mail saved")

case MFMailComposeResultSent: // 发送成功

print("Result: Mail sent")

case MFMailComposeResultFailed: // 发送失败

print("Result: Mail sending failed")

default:// 其他

print("Result: Mail not sent")

}

}

// MARK: - MFMessageComposeViewControllerDelegate

func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {

print(controller.attachments) // 所有附件

// 关闭MFMessageComposeViewController

controller.dismissViewControllerAnimated(true, completion: nil)

switch result { // 发送状态

case MessageComposeResultCancelled:

print("Result: Mail sending cancelled") // 取消发送

case MessageComposeResultSent: // 发送成功

print("Result: Mail sent")

case MessageComposeResultFailed: // 发送失败

print("Result: Message sending failed")

default:// 其他

print("Result: Message not sent")

}

}

Swift 发送邮件和发短信的更多相关文章

  1. 打电话,发短信,发邮件,app跳转

    1.打电话 - (IBAction)callPhone1:(id)sender { NSURL *url = [NSURL URLWithString:@"tel://18500441739 ...

  2. iOS中如何切换到发短信、打电话、发邮件

    我们在做APP的时候,难免会遇到需要调用短信,电话等程序的时候.如美团. 当然,这些都只是一些简单的方法就可以实现,但是时间久了也会淡忘,所以想写这边博客.一是为了再捡起来复习一下,另一个相当于留个备 ...

  3. 打电话、发短信、web以及发邮件

    #import "ViewController.h" #import <MessageUI/MessageUI.h> //导入信息UI库 @interface View ...

  4. IOS 开发,调用打电话,发短信,打开网址

    IOS 开发,调用打电话,发短信,打开网址   1.调用 自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString: ...

  5. html页面通过特殊链接:打电话,发短信,发邮件详细教程

    采用url href链接的方式,实现在Safari  ios,Android 浏览器,webos浏览器,塞班浏览器,IE,Operamini等主流浏览器,进行拨打电话功能. 1. 拨打电话 在电话号码 ...

  6. 移动设备wap手机网页html5通过特殊链接:打电话,发短信,发邮件详细教程

    如果需要在移动浏览器中实现拨打电话,调用sms发短信,发送email等功能,移动手机WEB页面(HTML5)Javascript提供的接口是一个好办法. 采用url href链接的方式,实现在Safa ...

  7. IOS,发短信,发邮件,打电话

    今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主要二种方法,下面我就分别说说它们的优缺点.1.1.发短信(1)——URL // ...

  8. 今天工作遇到要发短信(ios)的功能,于是随手记录了一下

    ios中发送短信有两种 1.程序外调用系统短信 2.程序内调用系统发短信 第一种比较简单,直接调用url就可以了 oc下的代码为 [[UIApplication sharedApplication] ...

  9. iOS - 打电话, 发短信

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...

随机推荐

  1. bzoj 3450: Tyvj1952 Easy

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 411  Solved: 309[Submit][Status][Discuss] Descriptio ...

  2. JSON 中JsonConfig的使用(转)

    我们通常对一个Json串和Java对象进行互转时,经常会有选择性的过滤掉一些属性值,而json-lib包中的JsonConfig为我们提供了这种 功能,具体实现方法有以下几种.(1)建立JsonCon ...

  3. Linux后台运行命令,nohup和&的区别

    &的意思是在后台运行, 什么意思呢?  意思是说, 当你在执行 ./a.out & 的时候, 即使你用ctrl C,  那么a.out照样运行(因为对SIGINT信号免疫). 但是要注 ...

  4. LeetCode——minimum-path-sum

    Question Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  5. LeetCode——Edit Distance

    Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...

  6. Win7旗舰版中的IIS配置asp.net 完美通过版,附代码 以及出现的 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\8d57d

    先解决问题:“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\8d57d 图: 其他的解决方案 ...

  7. webservice的cxf的客户端

    1.新建一个java项目 2.用cmd命令生成客户端的使用说明文档 wsdl2java -p xiaostudy -d . http://127.0.0.1:9998/number?wsdl 3.导入 ...

  8. pathway一些网站

    1.BioCarta_Pathways https://cgap.nci.nih.gov/Pathways/BioCarta_Pathways

  9. 检签 sub sup应用

    <html> <body> <b>This text is bold</b> <br /> <strong>This text ...

  10. 在Jupyter notebook中使用特定虚拟环境中的python的kernel

        在虚拟环境tf中安装完tensorflow后,在虚拟环境tf打开的jupyter里发现只有一个kernel-python3,新建一个文件, import tensorflow as tf ,发 ...