import MessageUI
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("邮件主题")
mail.setMessageBody("邮件内容", isHTML: true)
// 收件人
mail.setToRecipients(["xxx@xxx.com"])
// 抄送
mail.setCcRecipients(["xxx@xxx.com"])
// 密送
mail.setBccRecipients(["xxx@xxx.com"])
if let image = UIImage(named: "image"),
let data = image.jpegData(compressionQuality: 1) {
// 添加附件。 mimeType: http://www.iana.org/assignments/media-types/
mail.addAttachmentData(data, mimeType: "image/png", fileName: "image")
}
// 设置首选发送电子邮件地址
//mail.setPreferredSendingEmailAddress("xxx@xxx.com")
present(mail, animated: true, completion: nil)
// MARK: - MFMailComposeViewControllerDelegate
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
mail.dismiss(animated: true, completion: nil)
}
网友评论