我想从我的应用程序发送邮件。我正在用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)
}
答案 0 :(得分:1)
您是说要在幕后发送电子邮件,而用户却没有看到电子邮件视图控制器?如果这是您想要的,则无法从您的(前端)代码执行此操作。 Apple并不希望应用程序能够以自动方式发送电子邮件,而用户无需点击发送按钮。如果您愿意,可以对该功能进行编程,以便从您应用的服务器端发送电子邮件。