【问题标题】:java.io.IOException: Exception writing Multipart with SpringBootjava.io.IOException:使用 Spring Boot 编写 Multipart 时出现异常
【发布时间】:2022-01-12 15:54:23
【问题描述】:

我想发送一封带附件的电子邮件:

  public void sendMailWithAttachment(String to, String subject, String body, String fileToAttach) {

        MimeMessagePreparator preparator = mimeMessage -> {

            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            mimeMessage.setFrom(new InternetAddress("admin@gmail.com"));
            mimeMessage.setSubject(subject);
            mimeMessage.setText(body);

            FileSystemResource file = new FileSystemResource(new File(fileToAttach));
            System.out.println(file.contentLength());
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.addAttachment("logo.jpg", file);
        };

        try {
            javaMailSender.send(preparator);
        }
        catch (MailException ex) {
            // simply log it and go on...
            System.err.println(ex.getMessage());
        }
    }

但我有这个例外:

Failed messages: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.IOException: Exception writing Multipart

【问题讨论】:

    标签: java spring-boot file smtp attachment


    【解决方案1】:

    Spring documentation 中给出的示例与您的代码不匹配:它创建了一个 MimeMessageHelper 对象并使用它来定义正文和附件。

    你应该这样做:

    public void sendMailWithAttachment(String to, String subject, String body, String fileToAttach) {
    
            MimeMessagePreparator preparator = mimeMessage -> {
    
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setFrom(new InternetAddress("admin@gmail.com"));
                message.setSubject(subject);
                message.setText(body);
    
                FileSystemResource file = new FileSystemResource(new File(fileToAttach));
                message.addAttachment("logo.jpg", file);
            };
    
            try {
                javaMailSender.send(preparator);
            }
            catch (MailException ex) {
                // simply log it and go on...
                System.err.println(ex.getMessage());
            }
        }
    

    【讨论】:

      【解决方案2】:

      我会以不同的方式创建MimeMessagePreparator 并使用Streams 来读取文件。

      public void sendMailWithAttachment(String to, String subject, String body, String fileToAttach) {
      
              MimeMessagePreparator preparator = mimeMessage -> {
      
                  FileInputStream inputStream = new FileInputStream(new File(fileToAttach));
      
                  MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                  message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
                  message.setFrom(new InternetAddress("admin@gmail.com"));
                  message.setSubject(subject);
                  message.setText(body);            
                  message.addAttachment("logo.jpg", new ByteArrayResource(IOUtils.toByteArray(inputStream)));
        
              };
      
              try {
                  javaMailSender.send(preparator);
              }
              catch (MailException ex) {
                  // simply log it and go on...
                  System.err.println(ex.getMessage());
              }
          }
      

      【讨论】:

        【解决方案3】:

        这可能有多种原因。对我来说,这不是设置文本。在你的情况下,如果 body String 为空,你可能会遇到同样的问题。

        下面的答案帮助我弄清楚了。 https://stackoverflow.com/a/33015901/9905202

        【讨论】:

          猜你喜欢
          • 2018-05-17
          • 1970-01-01
          • 1970-01-01
          • 2018-03-05
          • 2020-05-28
          • 2016-06-26
          • 2019-02-17
          • 2019-04-09
          • 2018-09-07
          相关资源
          最近更新 更多