MessageUI(发送邮件)

在MessageUI库中发送邮件使用MFMailComposeViewController。

1 相关API

1.1 Determining Mail Availability

/// 能否发送邮件
@available(iOS 3.0, *)
public class func canSendMail() -> Bool

1.2 Setting Mail Fields Programmatically

/// 设置主题
@available(iOS 3.0, *)
public func setSubject(subject: String)

/// 收件人
@available(iOS 3.0, *)
public func setToRecipients(toRecipients: [String]?)

/// 抄送
@available(iOS 3.0, *)
public func setCcRecipients(ccRecipients: [String]?)

/// 密送
@available(iOS 3.0, *)
public func setBccRecipients(bccRecipients: [String]?)

/// 设置内容,可设置html格式的内容
@available(iOS 3.0, *)
public func setMessageBody(body: String, isHTML: Bool)

/// 添加附件
@available(iOS 3.0, *)
public func addAttachmentData(attachment: NSData, mimeType: String, fileName filename: String)

1.3 Accessing the Delegate

/// 监听发送结果
unowned(unsafe) public var mailComposeDelegate: MFMailComposeViewControllerDelegate?

2 实战演练

2.1 源代码

//
//  ViewController.swift
//  Message
//
//  CSDN:http://blog.csdn.net/y550918116j
//  GitHub:https://github.com/937447974/Blog
//
//  Created by yangjun on 16/1/11.
//  Copyright © 2016年 阳君. All rights reserved.
//

import UIKit
import MessageUI

class ViewController: UIViewController,  MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // 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: -  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")
        }
    }

}


 


Appendix

Sample Code

Swift

Message UI Framework Reference

MFMailComposeViewController Class Reference

Revision History

时间描述
2016-01-12博文完成

CSDN:http://blog.csdn.net/y550918116j

GitHub:https://github.com/937447974/Blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值