使用Javamail发送带有附件的邮件时出现FileNotFound

时间:2019-05-24 09:21:37

标签: java spring javamail

我正在尝试使用Javamail发送带有附件文件的邮件。但是,当代码尝试读取文件的内容时,将引发FileNoFound异常。 奇怪的是,在调试模式下,我可以看到它获取了文件(MultiPartFile变量不是empy)。我也在使用@Async,所以也许这是真正的问题。

我的方法sendMail:

@Async("threadPoolTaskExecutor")
    public void sendMail(Context ctx, String dest, String subject, String templateName, MultipartFile attachment)
            throws MessagingException, MailException, IOException {

        MimeMessage mimeMessage = emailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        message.setFrom("test@test.com");
        message.setTo(dest);
        message.setSubject(subject);

        String htmlContent = emailTemplateEngine.process(templateName, ctx);
        message.setText(htmlContent, true); // true = isHtml

        if (attachment != null) {
            InputStreamSource attachmentSource;
            attachmentSource = new ByteArrayResource(attachment.getBytes());
            message.addAttachment(attachment.getOriginalFilename(), attachmentSource);
        }

        emailSender.send(mimeMessage);      

    }

我遇到的错误是:

java.io.FileNotFoundException: C:\Users\user\AppData\Local\Temp\tomcat.970471969296194243.8080\work\Tomcat\localhost\ROOT\upload_a28782c3_18d7_4b6b_84b7_2d9e81e9a692_00000007.tmp 
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:194)
    at org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:100)
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getBytes(StandardMultipartHttpServletRequest.java:245)
[...]

编辑:我尝试删除@Async,文件上传没有任何错误。所以这是导致我的FileNotFound异常的原因...那么有什么办法可以解决这个问题,因为我非常想将此方法置于异步状态。我对异步和线程管理了解不多,所以也许我只是在上传文件时无法使用它(?)

1 个答案:

答案 0 :(得分:0)

您的代码似乎无法解释emailTemplateEngine是什么。 通常,您的服务器默认情况下不会复制自定义模板。检查目录C:\Users\user\AppData\Local\Temp\tomcat.970471969296194243.8080\work\Tomcat\localhost\ROOT\ 它应该是开箱即用的。 您可以尝试使用freemarker.template.Configuration

private Configuration initialiseFreeMarkerConfiguration() throws IOException, TemplateException {
    FreeMarkerConfigurationFactoryBean factory = new FreeMarkerConfigurationFactoryBean();
    factory.setTemplateLoaderPath("classpath:/ROOT/");
    factory.setPreferFileSystemAccess(false);
    return factory.createConfiguration();
  }

您的代码...

  private final Configuration freeMarkerConfiguration;
// in method
 MimeMessage mimeMessage = emailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        message.setFrom("test@test.com");
        message.setTo(dest);
        message.setSubject(subject);
message.setText(processEmailFromTemplate(model), true);

//attachment logic, use FileSystemResource  instead of ByteArrayResource
if (attachment != null) {
            InputStreamSource attachmentSource;
            FileSystemResource file = new FileSystemResource(new File(attachment));
            message.addAttachment(attachment.getOriginalFilename(), file );
        }

 emailSender.send(mimeMessage); 

方法processEmailFromTemplate()应该做类似的事情。

private String processEmailFromTemplate(Map<String, Object> model) throws IOException, TemplateException {
StringBuilder content = new StringBuilder();
content.append(FreeMarkerTemplateUtils.processTemplateIntoString(freeMarkerConfiguration.getTemplate("your template name"), model));
return content.toString();
}