【问题标题】:Tried to send email from in-app and doesn't work - Swift (iOS)尝试从应用内发送电子邮件但不起作用 - Swift (iOS)
【发布时间】:2015-11-05 13:46:52
【问题描述】:

我尝试了 2 个来自不同网站的代码来从我的 iOS 应用发送电子邮件。 当我按下 Send 按钮时,它会调用方法 ma​​ilComposeController 并始终返回日志“已发送邮件”,因为 result 参数始终为 MFMailComposeResultSent.value,即使我的 iPhone 5S 处于飞行模式

除此之外,即使我有互联网连接,我也从未收到过电子邮件。已经检查了垃圾邮件和其他文件夹,如果延迟则等待一整天,但即使尝试了几次也没有收到。

从 2014 年中期开始,我使用 Swift 编程并在 Macbook Pro 视网膜上使用 XCode 6.4 和 Yosemite 10.10.5。

这里是 Swift 代码:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        if (MFMailComposeViewController.canSendMail()) {

            var emailTitle = "Vea Software Feedback"

            var messageBody = "Vea Software! :)"

            var toRecipents = ["oscar.muntal@gmail.com"]

            var mc: MFMailComposeViewController = MFMailComposeViewController()

            mc.mailComposeDelegate = self

            mc.setSubject(emailTitle)

            mc.setMessageBody(messageBody, isHTML: false)

            mc.setToRecipients(toRecipents)

            self.presentViewController(mc, animated: true, completion: nil)

        }else {

            println("No email account found")

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Email Delegate
    func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {

        switch result.value {
        case MFMailComposeResultCancelled.value:
            println("Mail cancelled")
        case MFMailComposeResultSaved.value:
            println("Mail saved")
        case MFMailComposeResultSent.value:
            println("Mail sent")
        case MFMailComposeResultFailed.value:
            println("Mail sent failure: \(error.localizedDescription)")
        default:
            break
        }
        self.dismissViewControllerAnimated(false, completion: nil)

    }
}

非常感谢您提前提供的帮助。

【问题讨论】:

    标签: ios iphone swift email in-app


    【解决方案1】:

    对我来说,以下代码可以正常工作:

    import UIKit
    import MessageUI
    
    class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
      override func viewDidLoad() {
        super.viewDidLoad()
    
        if MFMailComposeViewController.canSendMail() {
          let toRecipents = ["oscar.muntal@gmail.com"]
          let emailTitle = "Vea Software Feedback"
          let messageBody = "Vea Software! :)"
    
          let mc: MFMailComposeViewController = MFMailComposeViewController()
          mc.mailComposeDelegate = self
          mc.setToRecipients(toRecipents)
          mc.setSubject(emailTitle)
          mc.setMessageBody(messageBody, isHTML: false)
    
          presentViewController(mc, animated: true, completion: nil)
        } else {
          print("cannot send mails")
        }
      }
    
      func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        switch result {
        case MFMailComposeResultCancelled:
          print("Mail cancelled")
        case MFMailComposeResultSaved:
          print("Mail saved")
        case MFMailComposeResultSent:
          print("Mail sent")
        case MFMailComposeResultFailed:
          print("Mail sent failure: \(error?.localizedDescription)")
        default:
          break
        }
    
        dismissViewControllerAnimated(true, completion: nil)
      }
    }
    

    【讨论】:

    • 顺便说一句:你是在模拟器还是真机上测试?
    • 我正在我的真机 iPhone 5S 上进行测试。
    • 您是否在电子邮件收件箱中收到了发送的消息? (在您粘贴的代码中有我的电子邮件)。
    • 当然我把我的电子邮件放进去。 :) 我收到了邮件,是的。你试过我的代码吗?特别是我在 mailcomposecontroller 委托方法中更改了一些东西...
    • 你用let,op用var
    【解决方案2】:

    感谢 Andre Slotta,我解决了我的问题。

    问题是我没有在设置 > 邮件、通讯录、日历 > myemailaccount 中启用“邮件”开关。

    这使得可以从本机 iOS 应用发送和接收电子邮件。而不是这个,我只是为“联系人”、“日历”和“笔记”启用了这个,并在我的 iPhone 中使用 Gmail iOS 应用程序来发送电子邮件。

    它也适用于我的初始代码,所以这根本不是代码问题,每个人都可以在 Swift(不是 Swift2)中尝试我的初始代码。

    再次感谢安德烈·斯洛塔的帮助!!!

    【讨论】:

      【解决方案3】:

      请在您的手机设置中添加您的邮箱!

      【讨论】:

        猜你喜欢
        • 2017-10-15
        • 2013-04-17
        • 1970-01-01
        • 2017-12-23
        • 1970-01-01
        • 1970-01-01
        • 2020-08-30
        • 2012-01-06
        • 1970-01-01
        相关资源
        最近更新 更多