Sending an email from swift 3

前端 未结 5 2064
广开言路
广开言路 2020-12-25 09:39

I am trying to set up an app with send email option.

I have this code:

import Foundation
import MessageUI
import UIKit

class emailClass: UIViewContr         


        
5条回答
  •  臣服心动
    2020-12-25 10:27

    Sending an Email is quit easy in Swift 5 you need to confirm and implement MFMailComposeViewControllerDelegate and check if we can send email on this device

    Here is the tiny piece of code I was using for my task

    import UIKit
    import MessageUI
    
    class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        //MARK: IBAction Method for Button click
        @IBAction func sendEmail(_ sender: Any) {
            //TODO:  You should chack if we can send email or not
            if MFMailComposeViewController.canSendMail() {
                let mail = MFMailComposeViewController()
                mail.mailComposeDelegate = self
                mail.setToRecipients(["you@yoursite.com"])
                mail.setSubject("Email Subject Here")
                mail.setMessageBody("

    You're so awesome!

    ", isHTML: true) present(mail, animated: true) } else { print("Application is not able to send an email") } } //MARK: MFMail Compose ViewController Delegate method func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true) } }

    PS: Please don't forget you need to test this on real device

提交回复
热议问题