請嘗試以下代碼電子郵件HTML正文裏面插入圖片。
代碼
雨燕4.0 &
雨燕3.1
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate{
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - Send Email
@IBAction func actionSendEmail(_ sender: Any) {
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
let image = UIImage(named:"testImage") // Your Image
let imageData = UIImagePNGRepresentation(image!) ?? nil
let base64String = imageData?.base64EncodedString() ?? "" // Your String Image
let strHtml = "<html><body><p>Header: Hello Test Email</p><p><b><img src='data:image/png;base64,\(String(describing: base64String))'></b></p></body></html>"
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["[email protected]"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody(strHtml, isHTML: true)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
//MARK:- MFMailCompose Delegate Methods
func mailComposeController(_ controller: MFMailComposeViewController,
didFinishWith result: MFMailComposeResult, error: Error?) {
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
}
你見過這個https://開頭計算器。 COM /問題/ 12210571 /如何到添加-A-圖像輸入電子郵件-體使用,mfmailcomposeviewcontrolle r –
我不認爲你不能用HTML標籤發送圖像。你只能作爲附件發送 –
Thak你。我在電子郵件前景html中做過,但我知道圖像源,在Swift Xcode中,我知道哪裏是si? (Src = ??????) – Gilko