Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending emails with attachments - empty multipart

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 ?

like image 341
Przemek85 Avatar asked Aug 31 '15 08:08

Przemek85


1 Answers

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());
}
like image 134
Amit Bhati Avatar answered Sep 23 '22 22:09

Amit Bhati