oscar
oscar

Reputation: 189

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

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 = ["[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)

    }
}

Thank you very much for your help in advance.

Upvotes: 2

Views: 3395

Answers (3)

Shakeel Ahmed
Shakeel Ahmed

Reputation: 6051

Please add you email account in your mobile phone Setting!

Upvotes: 0

oscar
oscar

Reputation: 189

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

Upvotes: 1

André Slotta
André Slotta

Reputation: 14040

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 = ["[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)
  }
}

Upvotes: 3

Related Questions