java.io.IOException: Exception writing Multipart with SpringBoot

Viewed 662

I want to send an email with attachment:

  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
3 Answers

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.

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.

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

There can be multiple causes for this. For me, it was not setting the text. In your case, if the body String is null, you may have the same problem.

The below answer helped me figure it out. https://stackoverflow.com/a/33015901/9905202

Related