无法使用 Spring Boot 发送带有附件的电子邮件

问题描述

在电子邮件中发送多部分文件时出错。

org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.FileNotFoundException: /tmp/tomcat.8099.7871695435416015654/work/Tomcat/localhost/ROOT/upload_090af3f0_4090_48f5_b9fa_4724022873bb_00000000.tmp (No such file or directory)

我正在使用 Ubuntu Server 来运行我的 java Spring boot(2.4.0) Jar 文件。这是我的代码

 @Async
 public void sendMessageWithAttachment(
            EmailMapper emailMapper,List<multipartfile> files) throws MessagingException {
        // ...
        if (emailMapper != null) {
            List<Subscriber> subscribers = kaleService.getAllSubscribers();

            for (int index = 0; index < subscribers.size(); index++) {


                MimeMessage message = emailSender.createMimeMessage();

                MimeMessageHelper helper = new MimeMessageHelper(message,true);

                helper.setTo(subscribers.get(index).getEmail());
                helper.setFrom(kale_email_from);
                helper.setSubject(emailMapper.getSubject());
                helper.setText(emailMapper.getDescription());
                if(files != null ) {
                    for (int indexOfFiles = 0; indexOfFiles < files.size(); indexOfFiles++) {
                        helper.addAttachment(files.get(indexOfFiles).getoriginalFilename(),files.get(indexOfFiles));
                    }
                }
                System.out.println("Sending email to  : " + subscribers.get(index).getEmail());
                emailSender.send(message);

            }
         
        }
    }
  

发送没有多部分文件的电子邮件工作正常

Spring Boot 2.4.0 版 ubuntu服务器

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)