aisuan
aisuan

Reputation: 1

How can I make an app send an email?

I have found code on the web and this site.

I have updated the deprecated parts.

The mail appears to be sent but never turns up. Does something need to be set outside the app somewhere or is the code faulty. App has one button that sends the email.

import Foundation
import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBAction func pressedSendMail(sender: AnyObject) {
    let mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.presentViewController(mailComposeViewController, animated: true, completion: nil)
    } else {
        self.showSendMailErrorAlert()
    }
}

func configuredMailComposeViewController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

    mailComposerVC.setToRecipients(["[email protected]"])
    mailComposerVC.setSubject("Sending you an in-app e-mail...")
    mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

    return mailComposerVC
}

func showSendMailErrorAlert() {
    let alert = UIAlertController(title: "Could Not Send Email",
        message: "Your device could not send e-mail.  Please check e-mail configuration and try again.",
        preferredStyle: .Alert)
    let action = UIAlertAction(title: "OK",
                               style: .Default,
                               handler: nil)
    alert.addAction(action)
    presentViewController(alert, animated: true, completion: nil)
}

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Upvotes: 0

Views: 298

Answers (2)

aisuan
aisuan

Reputation: 1

I was testing on a device (iPad mini) it was not was just not sending the email. Turns out I have never sent an email from this device. So mail was not set up. The iPhone gives an alert in this situation the iPad does not. If you have problems like this check your user has sent email from the device, not just a web browser on the device.

Upvotes: 0

Trenton Tyler
Trenton Tyler

Reputation: 534

Similar to the Phone and Camera application, the email application will not open in the simulator. You will need to test it on a physical device in order to test it. From looking at the code you gave us it should work fine. Just connect your iPhone or another Apple device and run it on there to see it work.

Upvotes: 4

Related Questions