直接从应用程序发送电子邮件 - Swift

Send email from an App directly - Swift

我想从我的应用程序发送邮件。我正在用 Swift 做我的第一步,我卡在了一个点上。我想按下一个按钮并直接发送电子邮件。我想从我的 TextField 中获取 Subject 和 MessageBody 的数据。包括来自我的 TextField 的发件人数据(请查看屏幕截图) 怎么做? 这是我的代码:

@IBAction func sendEmailButtonTapped(sender: AnyObject) {
    let mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.present(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(["primer@gmail.com"])
    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 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")
    sendMailErrorAlert.show()
}

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

你是说你想在后台发送电子邮件,而不让用户看到电子邮件视图控制器出现?如果那是你想要的,你不能从你的(前端)代码中做到这一点。 Apple 不希望应用程序能够在用户不点击发送按钮的情况下自动发送电子邮件。如果您愿意,您可以对功能进行编程以从应用程序的服务器端发送电子邮件。