Tried to send email from in-app and doesn't work - Swift (iOS)

爱⌒轻易说出口 提交于 2019-12-02 18:53:27

问题


I tried 2 codes from different sites to send an email from my iOS app. When I press the Send button it calls the method mailComposeController and always returns me the log "Mail sent" as the result parameter is always MFMailComposeResultSent.value, even when I have my iPhone 5S in airplane mode.

Besides this, even when I have internet connection, I never receive the email. Already checked SPAM and other folders and waited one entire day if it was delayed but never received even trying several times.

I program with Swift and use XCode 6.4 in a Macbook Pro retina from mid-2014 with Yosemite 10.10.5.

Here the Swift code:

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)

    }
}

Thank you very much for your help in advance.


回答1:


for me the following code works perfectly fine:

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



回答2:


Thanks to Andre Slotta I solved my problem.

The problem was that I didn't have enabled the "Mail" Switch in the Settings > Mail, Contacts, Calendar > myemailaccount.

This enables to send and receive emails from the native iOS app. Instead of this I just had this enabled for the "Contacts", "Calendars" and "Notes", and use the Gmail iOS app for the Email purposes in my iPhone.

It worked with my initial code also, so it wasn't a code problem at all and everyone can try my initial code in Swift (not Swift2).

Thanks again Andre Slotta for your help!!!




回答3:


Please add you email account in your mobile phone Setting!



来源:https://stackoverflow.com/questions/33546239/tried-to-send-email-from-in-app-and-doesnt-work-swift-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!