Reputation: 37
I try to send an email through my app but I got an error that called
"Please set up mail account in order to send email".
My code block is below.
import MessageUI
@IBAction func emailTapped(_ sender: Any) {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["abc@gmail.com"])
mailComposerVC.setSubject("Sending you an in-app e-mail...")
mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
if MFMailComposeViewController.canSendMail() {
self.present(mailComposerVC, animated: true, completion: {() -> Void in })
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Swift.Error?){
controller.dismiss(animated: true) { () -> Void in }
}
Upvotes: 3
Views: 5777
Reputation: 1805
The code looks good enough (though it can be improved), you need to setup your email account on the device you are using.
import MessageUI
@IBAction func emailTapped(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["abc@gmail.com"])
mailComposerVC.setSubject("Sending you an in-app e-mail...")
mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
self.present(mailComposerVC, animated: true, completion: {() -> Void in })
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Swift.Error?){
controller.dismiss(animated: true) { () -> Void in }
}
Now, search for the Settings
app on your iPhone, then goto Mail
-> Accounts
-> Add Account
to setup a email account on the phone.
You can check out this video.
Upvotes: 4