Mus*_*non 5 macos swift swift3
我搜索解决方案发送带附件的邮件.我有这个代码,但文件没有附加...
if let url = URL(string: "mailto:\(email)?subject=report&body=see_attachment&attachment=/Users/myname/Desktop/report.txt") {
NSWorkspace.shared().open(url)
}
Run Code Online (Sandbox Code Playgroud)
我看到它可能与MessageUI一起工作,但我无法导入这个框架,我不知道为什么.我收到此错误消息:没有这样的模块'MessageUI'我在General> Linked Frameworks and Libraries中检查过,但是没有MessageUI ...
有人有解决方案在邮件中添加文件吗?谢谢
看来,attachment
在mailto:
网址中不支持在Mac OS(并不总是至少...细节粗劣取决于你往哪里看互联网上的:))
你可以使用我从这篇博文中找到的内容,这是一个记录在这里的实例NSSharingService
这是一个演示如何使用它的示例.
在你的情况下,你可以做类似的事情:
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)
Run Code Online (Sandbox Code Playgroud)
希望对你有所帮助.
import MessageUI
class ViewController: UIViewController,MFMailComposeViewControllerDelegate {
func sendMail() {
if( MFMailComposeViewController.canSendMail()){
print("Can send email.")
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
//Set to recipients
mailComposer.setToRecipients(["yakupad@yandex.com"])
//Set the subject
mailComposer.setSubject("email with document pdf")
//set mail body
mailComposer.setMessageBody("This is what they sound like.", isHTML: true)
let pathPDF = "\(NSTemporaryDirectory())contract.pdf"
if let fileData = NSData(contentsOfFile: pathPDF)
{
print("File data loaded.")
mailComposer.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: "contract.pdf")
}
//this will compose and present mail to user
self.present(mailComposer, animated: true, completion: nil)
}
else
{
print("email is not supported")
}
func mailComposeController(_ didFinishWithcontroller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
{
self.dismiss(animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11030 次 |
最近记录: |