如何使用 JavaMail 发送非本地附件

发布于 2024-11-19 07:11:56 字数 398 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知足的幸福 2024-11-26 07:11:56

我想通了。 fakepath 是浏览器中的一项安全功能。但使用 tomcat 时,该文件实际上存储在 tomcat 文件夹内的临时文件夹中。所以我只需要使用 tomcat 库 commons.fileupload,然后使用它从文件中提取数据,而不管 fakepath 位置如何。

//Handle File Upload for the attachment
           ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());

            try{
            List fileItemsList = servletFileUpload.parseRequest(request);

            //TODO: Take datafile input from the field and pass the file name so that we can view the file name

            Iterator it = fileItemsList.iterator();
            while (it.hasNext()){
              FileItem fileItem = (FileItem)it.next();
              if (fileItem.isFormField()){
                /* The file item contains a simple name-value pair of a form field */
              }
              else{ //do what you want with the file}

然后,我将其传递到我的邮件实用程序,将文件名更改为正确的名称以获得正确的扩展名,并且它起作用了。当然,您必须将表单编码为多部分表单,并且还必须将 Mime 消息设为多部分。但毕竟它相当简单。

    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent(body, "text/html");



    MimeBodyPart attachFilePart = new MimeBodyPart();
    FileDataSource fds = 
        new FileDataSource(file);
    attachFilePart.setDataHandler(new DataHandler(fds));
    attachFilePart.setFileName(fileName);


    Multipart mp = new MimeMultipart();
    mp.addBodyPart(textPart);
    mp.addBodyPart(attachFilePart);

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.

//Handle File Upload for the attachment
           ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());

            try{
            List fileItemsList = servletFileUpload.parseRequest(request);

            //TODO: Take datafile input from the field and pass the file name so that we can view the file name

            Iterator it = fileItemsList.iterator();
            while (it.hasNext()){
              FileItem fileItem = (FileItem)it.next();
              if (fileItem.isFormField()){
                /* The file item contains a simple name-value pair of a form field */
              }
              else{ //do what you want with the file}

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.

    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setContent(body, "text/html");



    MimeBodyPart attachFilePart = new MimeBodyPart();
    FileDataSource fds = 
        new FileDataSource(file);
    attachFilePart.setDataHandler(new DataHandler(fds));
    attachFilePart.setFileName(fileName);


    Multipart mp = new MimeMultipart();
    mp.addBodyPart(textPart);
    mp.addBodyPart(attachFilePart);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文