gpt4 book ai didi

xcode - 如何让应用程序发送电子邮件?

转载 作者:行者123 更新时间:2023-11-28 10:19:41 26 4
gpt4 key购买 nike

我在网上和这个网站上找到了代码。

我已经更新了弃用的部分。

邮件似乎已发送但从未出现。是否需要在应用程序外部的某处设置某些内容,或者代码是否有问题。应用程序有一个发送电子邮件的按钮。

import Foundation
import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBAction func pressedSendMail(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(["myemail@hotmail.com"])
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 alert = UIAlertController(title: "Could Not Send Email",
message: "Your device could not send e-mail. Please check e-mail configuration and try again.",
preferredStyle: .Alert)
let action = UIAlertAction(title: "OK",
style: .Default,
handler: nil)
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
}

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

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

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

}

最佳答案

与电话和相机应用程序类似,电子邮件应用程序不会在模拟器中打开。您需要在物理设备上对其进行测试才能对其进行测试。通过查看您提供给我们的代码,它应该可以正常工作。只需连接您的 iPhone 或其他 Apple 设备并在其上运行即可查看其工作情况。

关于xcode - 如何让应用程序发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36605170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com