我在从 osx swift 应用程序发送邮件时遇到问题。为了发送邮件,我使用了下面的代码
import Foundation
import Cocoa
class sendemail : NSObject, NSSharingServiceDelegate{
func sendEmail() throws
{
print("enter email sending")
let body = "This is an email for auto testing throug code."
let shareItems = [body] as NSArray
let service = NSSharingService(named: NSSharingServiceNameComposeEmail)
service?.delegate = self
service?.recipients = ["[email protected] /cdn-cgi/l/email-protection"]
let subject = "Vea Software"
service?.subject = subject
service?.performWithItems(shareItems as [AnyObject])
}
}
我找到了此链接的来源:https://www.veasoftware.com/posts/send-email-in-swift-xcode-62-os-x-1010-tutorial https://www.veasoftware.com/posts/send-email-in-swift-xcode-62-os-x-1010-tutorial
但它不起作用。
我还尝试按照以下说明从终端发送邮件:
http://www.developerfiles.com/how-to-send-emails-from-localhost-mac-os-x-el-capitan/ http://www.developerfiles.com/how-to-send-emails-from-localhost-mac-os-x-el-capitan/
它说 :
postfix/postfix-script: fatal: the Postfix mail system is not running
请帮我。
我可以从已配置的 Mac 邮件应用程序手动发送邮件。
我在用
xcode 7.3, osx el captain and swift 2.2
现代斯威夫特:
func sendEmail(to recipients: [String], subject: String, body: String) {
let service = NSSharingService(named: .composeEmail)!
service.recipients = recipients
service.subject = subject
service.perform(withItems: [body])
}
// Usage
sendEmail(
to: ["[email protected] /cdn-cgi/l/email-protection"],
subject: "Vea software",
body: "This is an email for auto testing through code."
)
本地用户必须拥有 Mail.app 设置的帐户。您还需要一个实例NSApplication
运行这个。无法在 CLI 应用程序中执行此操作。如果您在控制台中看到以下错误,则意味着您没有活动的NSApplication
实例。
[default] 0 is not a valid connection ID
原答案
这对我有用:
import Cocoa
class SendEmail: NSObject {
static func send() {
let service = NSSharingService(named: NSSharingServiceNameComposeEmail)!
service.recipients = ["[email protected] /cdn-cgi/l/email-protection"]
service.subject = "Vea software"
service.performWithItems(["This is an email for auto testing through code."])
}
}
Usage:
SendEmail.send()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)