简体   繁体   English

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

[英]How to send a non-local attachment with JavaMail

I'm building an application using jsp's, servlets, and all that fun stuff.我正在使用 jsp、servlets 和所有有趣的东西构建一个应用程序。 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.现在,我有一个表单,它将表单中的所有信息传递给 html email,该表单使用 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.我将这个输入值传递给我的 servlet 并尝试发送 email。 The problem is that when the file is sending, the servlet cannot locate the file because this tag gives it the path问题是当文件发送时,servlet 无法定位文件,因为这个标签给了它路径

C:\fakepath\file.doc

Any help would be amazing.任何帮助都会很棒。

I figured it out.我想到了。 The fakepath was a security feature in browsers. fakepath 是浏览器中的一项安全功能。 What happens though with tomcat is that the file is actually stored in a temp folder inside the tomcat folder. tomcat 发生的情况是该文件实际上存储在 tomcat 文件夹内的临时文件夹中。 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.所以我只需要使用 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}

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.当然,您必须将表单编码为多部分表单,并且还必须使 Mime 消息多部分。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM