简体   繁体   English

java.io.IOException:使用 SpringBoot 写入 Multipart 时出现异常

[英]java.io.IOException: Exception writing Multipart with SpringBoot

I want to send an email with attachment:我想发送一个带有附件的 email:

  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());
        }
    }

but I have this Exception:但我有这个例外:

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

The example given in the Spring documentation doesn't match your code: it creates a MimeMessageHelper object and uses it to define both the body and the attached file. Spring 文档中给出的示例与您的代码不匹配:它创建一个MimeMessageHelper object 并使用它来定义正文和附加文件。

You should do something like this instead:你应该这样做:

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());
        }
    }

I would have created the MimeMessagePreparator in a different way and used Streams to read file.我会以不同的方式创建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());
        }
    }

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

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