ios - "Please set up mail account in order to send email"Swift 错误

标签 ios iphone swift email

我尝试通过我的应用程序发送电子邮件,但我收到一个名为

的错误

"Please set up mail account in order to send email".

我的代码块在下面。

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 } 
}

最佳答案

代码看起来不错(虽然还可以改进),您需要在您使用的设备上设置您的电子邮件帐户。

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 }

        }

现在,在您的 iPhone 上搜索 Settings 应用程序,然后转到 Mail -> Accounts -> Add Account 在手机上设置电子邮件帐户。 你可以看看这个video .

关于ios - "Please set up mail account in order to send email"Swift 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42276721/

相关文章:

android - 如何从付费应用转换为应用内购买? iOS和Android + Flutter

iphone - 闹钟iOS

ios - 将父容器的平移手势传递给嵌套的 UICollectionView

swift - 将个人资料图像字符串从 firebase 数据库下载到 uiimage

swift - MKOverlay 无法正确呈现接近第 180 条子午线(快速)

ios - swift : Pass TableViewCell text to new ViewController's cell

ios - 更新内容大小高度

ios - 将图像从 ViewController 传递到 ViewController

iphone - 你如何阻止 UITapGestureRecognizer 捕捉每一次点击?

ios - 订阅 self 是什么意思?