简体   繁体   English

如何在Swift中打开邮件面板? 苹果系统

[英]How to open mail panel in Swift? Mac OS

For the past few days I've been strugling with finding a way to send emails from my app. 在过去的几天里,我一直在努力寻找一种从我的应用程序发送电子邮件的方法。 This is the sample code I am currently using. 这是我当前正在使用的示例代码。 It think, by using this method, I might be unable to handle the situation of an improper setting of the mail accound of the computer my app will be running, so I thought about using a different method. 它认为,通过使用这种方法,我可能无法处理将要运行我的应用程序的计算机的邮件附件设置不当的情况,因此我考虑使用另一种方法。 I was wondering if is there any MessageUI equivalent for mac os. 我想知道是否有Mac OS的任何MessageUI等效项。 Is there any MessageUI equivalent for mac os. Mac上是否有任何MessageUI等效项。 Any ideas? 有任何想法吗?

There are two ways to do that: 有两种方法可以做到这一点:

  1. AppleScript framework AppleScript框架

  2. NSSharingService

NSSharingService to compose mail: NSSharingService撰写邮件:

let service = NSSharingService(named: NSSharingServiceNameComposeEmail)

service?.recipients = ["test@gmail.com"]
service?.subject = "Test Mail"
service?.performWithItems(["Test Mail body"])

Launch Mail App 启动邮件应用

If you just want to open Mail app, try below code: 如果您只想打开Mail应用程序,请尝试以下代码:

NSWorkspace.sharedWorkspace().launchApplication("Mail")

Improper Setting: 设置不当:

  • If account is not configured on the system, this code will show configuration window for mail 如果未在系统上配置帐户,则此代码将显示邮件的配置窗口
  • When user will try to send mail it'll be shown improper setting related error by Mail app so you need not to bother about it 当用户尝试发送邮件时,“邮件”应用会显示与设置不当相关的错误,因此您无需理会
  • If you want to use other then Mail app, just try your own mail system, native mail client or third party mail framework. 如果要使用其他而非Mail应用程序,则只需尝试使用自己的邮件系统,本机邮件客户端或第三方邮件框架。

Try from terminal: 从终端尝试:

You can use shell script to send mail using native client 您可以使用Shell脚本通过本机客户端发送邮件

mail -s "Hello" "test@gmail.com" <<EOF
Hello, Test!
EOF

For Swift 4, little changes. 对于Swift 4,变化不大。

let service = NSSharingService(named: NSSharingService.Name.composeEmail)
service?.recipients = ["test@gmail.com"]
service?.subject = "Test Mail"
service?.perform(withItems: ["Test Mail body"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM