ios 使用 Swift 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28963514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:09:19  来源:igfitidea点击:

Sending Email With Swift

iosswiftemailparse-platformmailgun

提问by Noah Barsky

how would you send an email with swift in an app. Like say for example your user want's to reset their password in a social media application with Parse(or not), but you don't wan't to use MessageUI because you want it to be automatic. I've done some research and found out that it can be possible with mailgun but i cannot figure out how to use it with swift and XCode 6. Can you please help me?

您将如何在应用程序中使用 swift 发送电子邮件。例如,您的用户希望使用 Parse(或不使用)在社交媒体应用程序中重置他们的密码,但您不想使用 MessageUI,因为您希望它是自动的。我做了一些研究,发现使用 mailgun 是可能的,但我不知道如何在 swift 和 XCode 6 中使用它。你能帮我吗?

回答by Mohammad Nurdin

Sure you can.

你当然可以。

import Foundation
import UIKit
import MessageUI

class ViewController: ViewController,MFMailComposeViewControllerDelegate {

    @IBAction func sendEmailButtonTapped(sender: AnyObject) {
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
            self.showSendMailErrorAlert()
        }
    }

    func configuredMailComposeViewController() -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

        mailComposerVC.setToRecipients(["[email protected]"])
        mailComposerVC.setSubject("Sending you an in-app e-mail...")
        mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

        return mailComposerVC
    }

    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    }

    // MARK: MFMailComposeViewControllerDelegate

    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        controller.dismissViewControllerAnimated(true, completion: nil)

    }
}

Source Andrew Bancroft

来源安德鲁班克罗夫特

回答by picciano

Parse supports both Mailgun and Mandrill out of the box. See their documentation

Parse 支持 Mailgun 和 Mandrill 开箱即用。查看他们的文档

You will need to write a CloudCode function, then call it from your app.

您需要编写一个 CloudCode 函数,然后从您的应用程序中调用它。

PFCloud.callFunctionInBackground("hello", withParameters:[:]) {
  (result: AnyObject!, error: NSError!) -> Void in
  if error == nil {
    // result is "Hello world!"
  }
}

Example code snippets to send mail using Mailgun.

使用 Mailgun 发送邮件的示例代码片段。

var Mailgun = require('mailgun');
Mailgun.initialize('myDomainName', 'myAPIKey');

Mailgun.sendEmail({
  to: "[email protected]",
  from: "[email protected]",
  subject: "Hello from Cloud Code!",
  text: "Using Parse and Mailgun is great!"
}, {
  success: function(httpResponse) {
    console.log(httpResponse);
    response.success("Email sent!");
  },
  error: function(httpResponse) {
    console.error(httpResponse);
    response.error("Uh oh, something went wrong");
  }
});