如何在swift上发送电子邮件?

时间:2017-06-22 19:11:01

标签: ios swift email development-environment

我在swift上发送电子邮件时跟踪了人们的代码,问题出现在我的手机上,发送按钮显示为灰色。我该怎么办?这是我的代码和手机上的问题图片。

The Problem On My Phone, The Send Button Is Greyed Out.

import UIKit
import MessageUI

class SendEmailViewController: UIViewController, MFMailComposeViewControllerDelegate {
    @IBAction func sendEmail(_ sender: Any)
    {
        let mailComposeViewController = configureMailController()
        if MFMailComposeViewController.canSendMail() {
            self.present(mailComposeViewController, animated: true, completion: nil)
        } else {
            showMailError()
        }
    } 

    func configureMailController() -> MFMailComposeViewController
    {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self

        mailComposerVC.setToRecipients(["abc@gmail.com"])
        mailComposerVC.setSubject("Gamifiction")
        mailComposerVC.setMessageBody("Hey, Check Out My Game", isHTML: false)

        return mailComposerVC    }

    func showMailError()
    {
        let sendMailErrorAlert = UIAlertController(title: "Could not send email", message: "Your device could not send email", preferredStyle: .alert)
        let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
        sendMailErrorAlert.addAction(dismiss)
        self.present(sendMailErrorAlert, animated: true, completion: nil)
    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:-1)

它不允许您在没有收件人信息的情况下发送。将电子邮件ID放在To:行。