How to Send Email using MFMailComposer

Send Email using MFMailComposer (Template) 1.) Import MessageUI       import MessageUI 2.) Show Email func showMail() { l...

Send Email using MFMailComposer (Template)

1.) Import MessageUI
      import MessageUI

2.) Show Email
func showMail() {
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
    func configuredMailComposeViewController() -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
        mailComposerVC.setMessageBody("", isHTML: false)
        return mailComposerVC
    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
    // MARK: MFMailComposeViewControllerDelegate Method
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        controller.dismissViewControllerAnimated(true, completion: nil)
        switch result.value {
        case MFMailComposeResultCancelled.value:
            println("Mail cancelled")
        case MFMailComposeResultSaved.value:
            println("Mail saved")
        case MFMailComposeResultSent.value:
            println("Mail sent")
            showAlertMessage("Your mail has been sent successfully")
        case MFMailComposeResultFailed.value:
            println("Mail sent failure: \(error.localizedDescription)")
            showAlertMessage("Your mail has not been sent successfully")
    func showAlertMessage(value : String){
        let alertController = UIAlertController(title: value, message:
            nil, preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)


xcode 5329815996324893865

Post a Comment Default Comments