如何使用 JavaMail 发送非本地附件
我正在使用 jsp、servlet 和所有有趣的东西构建一个应用程序。现在,我有一个表单,它将表单中的所有信息传递到使用 JavaMail API 发送的 html 电子邮件。它有效,但我正在尝试发送附件,而我现在设置的方式不起作用...
<div class="section">Upload Files: <input id="fileUpload" type="file" /></div>
我获取此输入的值,将其传递到我的 servlet 并尝试发送电子邮件。问题是,当发送文件时,servlet 无法找到该文件,因为该标记为其提供了路径
C:\fakepath\file.doc
。任何帮助都会很棒。
I'm building an application using jsp's, servlets, and all that fun stuff. Right now, I have a form that passes through all the information from the form to an html email that is sent using the JavaMail API. It works, but I am trying to send an attachment, and the way I have it set up right now does not work...
<div class="section">Upload Files: <input id="fileUpload" type="file" /></div>
I take this input's value, pass it through to my servlet and try to send the email. The problem is that when the file is sending, the servlet cannot locate the file because this tag gives it the path
C:\fakepath\file.doc
Any help would be amazing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。 fakepath 是浏览器中的一项安全功能。但使用 tomcat 时,该文件实际上存储在 tomcat 文件夹内的临时文件夹中。所以我只需要使用 tomcat 库 commons.fileupload,然后使用它从文件中提取数据,而不管 fakepath 位置如何。
然后,我将其传递到我的邮件实用程序,将文件名更改为正确的名称以获得正确的扩展名,并且它起作用了。当然,您必须将表单编码为多部分表单,并且还必须将 Mime 消息设为多部分。但毕竟它相当简单。
I figured it out. The fakepath was a security feature in browsers. What happens though with tomcat is that the file is actually stored in a temp folder inside the tomcat folder. So i just had to play with a tomcat library, commons.fileupload, and i used that to pull the data from the file, regardless of the fakepath location.
I then passed it through to my mail utility, changed the name of the file to the correct name to have the correct extension, and it worked. Of course, you have to encode the form as a multipart form, and you have to make the Mime Message multipart as well. But its fairly simple after all that.