Sending Email via Spring boot "spring-boot-starter-mail"

You have a second version of javax.mail.internet.MimeMessage on the classpath in addition to the one that's pulled in via spring-boot-starter-mail. A common culprit is Geronimo's JavaMail spec jar. Whichever jar it is, you need to exclude it from your application's dependencies. If you're not sure where it's coming from, running your application with -verbose:class will tell you.


My recommendation is to use the it.ozimov:spring-boot-email-core library, that hides all these implementations behind a single component called EmailService - well, I'm also developing the library :).

Your example would be:

@Autowired
public EmailService emailService;

public void sendEmail(){
   final Email email = DefaultEmail.builder()
        .from(new InternetAddress("[email protected]"))
        .replyTo(new InternetAddress("someone@localhost"))
        .to(Lists.newArrayList(new InternetAddress("someone@localhost")))
        .subject("Lorem ipsum")
        .body("Lorem ipsum dolor sit amet [...]")
        .encoding(Charset.forName("UTF-8")).build();

   emailService.send(email);
}

With the following application.properties:

spring.mail.host=your.smtp.com
spring.mail.port=587
spring.mail.username=test
spring.mail.password=test
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

It also supports some template engines, like Freemarker, Mustache and Pebble, while you can extend it to use more template engines. Moreover, it also supports email scheduling and prioritization (e.g. high priority for password recovery and low priority for newsletter.

There is an article on LinkedIn explaining how to use it. It is here.


Do not use javaMailSender.createMimeMessage(); try to use MimeMessagePreparator & MimeMessageHelper instead