OS X swift发送带附件的邮件

时间:2017-07-13 18:52:17

标签: swift macos email cocoa

我使用swift for osx。 我有这个发送邮件的代码:

let service = NSSharingService(named: NSSharingServiceNameComposeEmail)!
service.recipients = ["abc@domain.de"]
service.subject = "My Subject"
service.perform(withItems: ["My Message"])

但我想附上一个pdf文件。 我怎么能意识到它?

1 个答案:

答案 0 :(得分:2)

这项工作 see

let email = "your email here"
let path = "/Users/myname/Desktop/report.txt"
let fileURL = URL(fileURLWithPath: path)

let sharingService = NSSharingService(named: NSSharingServiceNameComposeEmail)
sharingService?.recipients = [email] //could be more than one
sharingService?.subject = "subject"
let items: [Any] = ["see attachment", fileURL] //the interesting part, here you add body text as well as URL for the document you'd like to share

sharingService?.perform(withItems: items)