Sending emails with attachments - empty multipart

11,340

The error was caused because you were

trying to send the mail with empty body Parts.

@Jen is right in his comment, you are setting the text of your mail incorrectly.

There is mistake in your code, change like below and try :-

MimeMessage mimeMessage = javaMailSender.createMimeMessage();
FileSystemResource file = new FileSystemResource(new File("c:\\simple.jpg"));
try {
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.addRecipient(Message.RecipientType.TO, new InternetAddress(clientEmail));
    helper.setFrom("[email protected]");
    helper.setText("Hi");
    helper.addAttachment(file.getFilename(), file);
} catch (MessagingException e) {
    e.printStackTrace();
}

try {
    this.javaMailSender.send(mimeMessage);
} catch (MailException ex) {
    // simply log it and go on...
    System.err.println(ex.getMessage());
}
Share:
11,340
Przemek85
Author by

Przemek85

Updated on June 15, 2022

Comments

  • Przemek85
    Przemek85 almost 2 years

    I stuck and can't figure out the problem with sending e-mails with attachments.

    Everything works fine without attachments. While I try to add attachment I get exception :

    Failed messages: javax.mail.MessagingException: IOException while sending message;nested exception is:
    java.io.IOException: javax.mail.MessagingException: Empty multipart: multipart/related; 
    boundary="----=_Part_1_733213598.1441009036818"
    

    Here is my properties file:

    [email protected] 
    spring.mail.host=smtp.gmail.com
    spring.mail.port=587
    [email protected]
    spring.mail.password=****
    spring.mail.properties.mail.mime.multipart.allowempty=true
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com
    

    and class using it :

    @Component
    public class MailSenderService {
    
        @Autowired
        JavaMailSender javaMailSender;
    
        @Value("${reports.mailSender.clientEmail}")
        private String clientEmail;
    
        public void sendMessage() {
    
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            FileSystemResource file = new FileSystemResource(new File("c:\\simple.jpg"));
            try {
                mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(clientEmail));
                mimeMessage.setFrom("[email protected]");
                mimeMessage.setText(
                        "Hi");
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.addAttachment(file.getFilename(), file);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
    
            try {
                this.javaMailSender.send(mimeMessage);
            } catch (MailException ex) {
                // simply log it and go on...
                System.err.println(ex.getMessage());
            }
        }
    }
    

    Guessing there is a problem with MimeMessageHelper, but can't figure out it alone. Can anyone try to help me ?

  • Przemek85
    Przemek85 over 8 years
    Thank you for helping, there is no more problem with exception - could you explain to me why there is no exception when I moved up MimeMessageHelper ? Another issue is that there is no attachment in receiving e-mail - dont know why. edit: everything is fine now - thank you guys for helping me - really appreciate it :)
  • Amit Bhati
    Amit Bhati over 8 years
    Its not about moving MimeMessageHelper , the problem was you were setting the content at the wrong place. You should set the things in helper and not in the mimeMessage.
  • Amit Bhati
    Amit Bhati over 8 years
    Check whether file is present at the specified location or not.
  • Przemek85
    Przemek85 over 8 years
    how can i honor you and Jens for answers ?