试图从应用程序发送电子邮件,但无法正常工作 - Swift(iOS)

问题描述 投票:0回答:3

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

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

我使用Swift进行编程,并在2014年中期使用Yosemite 10.10.5在Macbook Pro视网膜中使用XCode 6.4。

这里的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 = ["[email protected]"]

            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
3个回答
1
投票

对我来说,以下代码完美无缺:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
  override func viewDidLoad() {
    super.viewDidLoad()

    if MFMailComposeViewController.canSendMail() {
      let toRecipents = ["[email protected]"]
      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)
  }
}

0
投票

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

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

这样可以从本机iOS应用程序发送和接收电子邮件。取而代之的是,我只启用了“联系人”,“日历”和“笔记”,并在我的iPhone中使用Gmail iOS应用程序进行电子邮件。

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

再次感谢Andre Slotta的帮助!!!


0
投票

请在手机设置中添加电子邮件帐户!

© www.soinside.com 2019 - 2024. All rights reserved.