发送带有文件附件的邮件 [英] Send mail with file attachment

查看:177
本文介绍了发送带有文件附件的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索解决方案以发送带有附件的邮件. 我有此代码,但文件未附加...

if let url = URL(string: "mailto:\(email)?subject=report&body=see_attachment&attachment=/Users/myname/Desktop/report.txt") {
    NSWorkspace.shared().open(url)
}

我已经看到它也许可以与MessageUI一起使用,但是我无法导入这个框架,我也不知道为什么.我收到此错误消息:没有这样的模块'MessageUI' 我在常规">链接的框架和库"中签入,但没有MessageUI ...

有人有在邮件中添加文件的解决方案吗? 谢谢

解决方案

macOS似乎不支持mailto: URL中的attachment(并非总是至少...细节似乎很粗略,取决于您所查看的位置)互联网:))

您可以使用的是我从中发现的博客帖子,是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)

更新

@Spire在下面的评论中提到,这不会附加文件.

似乎有一个陷阱需要注意.

要使其正常工作,您需要研究自己的应用功能.

您可以:

  • 禁用应用沙箱
  • 为您要从中获取内容的文件夹启用读取权限.

我已经附上了几个屏幕截图.

如果我在功能"下禁用了应用程序沙箱",则会显示以下情况

这是我启用了应用程序沙箱并允许我的应用程序读取我的Downloads文件夹中的内容的图像

如果执行上述操作,则可以使用此URL访问位于我的下载文件夹中的名为document.txt的文件

let path = "/Users/thatsme/Downloads/document.txt"
let fileURL = URL(fileURLWithPath: path)

并将其附加到邮件

希望对您有帮助.

I search solution to send a mail with attachment. I have this code but the file is not attached...

if let url = URL(string: "mailto:\(email)?subject=report&body=see_attachment&attachment=/Users/myname/Desktop/report.txt") {
    NSWorkspace.shared().open(url)
}

I have see it maybe work with MessageUI, but I can't import this framework I don't know why. I get this error message : No such module 'MessageUI' I checked in General > Linked Frameworks and Libraries, but there are not MessageUI...

Anyone have a solution to add file in mail? Thanks

解决方案

It seems that attachment in mailto: URLs are not supported on macOS (not always at least...details seems sketchy dependent on where you look on the internet :))

What you can use instead I found out from this blog post, is an instance of NSSharingService documented here

Here is an example demonstrating how to use it.

And in your case you could do something like:

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)

Update

So @Spire mentioned in a comment below that this won't attach a file.

It seems there is a gotcha to be aware of.

For this to work you need to look into your App Capabilities.

You can either:

  • disable App Sandbox
  • enable read access for the folders from where you would like to fetch content.

I've attached a couple of screenshots.

Here is how this looks if I have disabled App Sandbox under Capabilities

And here is an image where I have enabled App Sandbox and allowed my app to read content in my Downloads folder

If I do the above, I can access my file called document.txt, located in my Downloads folder, using this URL

let path = "/Users/thatsme/Downloads/document.txt"
let fileURL = URL(fileURLWithPath: path)

And attach that to a mail

Hope that helps you.

这篇关于发送带有文件附件的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆