iOS开发 MFMailComposeViewController(发送邮件)的使用

一.需要导入messageUI.Frameworks(#import<messageUI/messageUI.h>)

二.步骤:

if MFMailComposeViewController.canSendMail() {
    let mailCompose = MFMailComposeViewController()
    // 收邮件人
    mailCompose.setToRecipients(["toRecipients@163.com"])
    // 抄送人
    mailCompose.setCcRecipients(["ccRecipients@163.com"])
    // 密送人
    mailCompose.setBccRecipients(["bccRecipients@163.com"])
    mailCompose.setSubject("邮箱主题")
    mailCompose.setMessageBody("邮箱内容", isHTML: false)
    // 添加一张图片
    if let addPic = UIImage(named: "") {
        //关于mimeType:http://www.iana.org/assignments/media-types/index.html
        mailCompose.addAttachmentData(UIImagePNGRepresentation(addPic)!, mimeType: "", fileName: "")
    }
    mailCompose.mailComposeDelegate = self
    self.present(mailCompose, animated: true, completion: nil)
} else {
    // 用户没有设置邮箱账号
    var str: String = ""
    str += "mailto:toRecipients@163.com"
    str += "?cc=ccRecipients@163.com"
    str += "&bcc=bccRecipients@163.com"
    str += "&subjec=邮箱主题"
    str += "&body=邮箱内容"
    let url = str.utf8.url!
    //"mailto:*@163.com?cc=second@example.com,third@example.com&subject=myemail!"
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.openURL(url)
    }
}

代理(MFMailComposeViewControllerDelegate):

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    print(result)
    controller.dismiss(animated: true, completion: nil)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值