This article is documents how to send email using Spring Boot over Microsoft 365 (formerly Office 365). Spring provides an easy to use interface called JavaMailSender that wraps the native JavaMail API. This example demonstrates sending both plain and HTML messages, as well as adding attachments.

Microsoft 365

Microsoft 365 subscriptions come with Outlook email access via browser or by native Office client applications. The Microsoft email solution (Outlook and Exchange) is ubiquitous for business users; It also offers the ability to connect from external clients for both sending and receiving messages. We will leverage this capability to send messages from a custom Java application.

JavaMailSender

Spring Boot provides this extended interface for JavaMail that supports MIME messages and completely handles session management. Messages containing attachments typically use this interface in conjunction with the MimeMessageHelper class.

Spring Boot Properties

Spring Boot pulls the mail settings from your default application.properties file (or YAML depending on your local setup). First set the properties to reference a Microsoft 365 (formerly Office 365) SMTP email host and enable security. Our GitHub repository should not contain sensitive property values, so externalize those variables.

// application.properties

spring.mail.host=smtp.office365.com
spring.mail.port=587
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=false
spring.mail.properties.mail.smtp.timeout=15000
spring.mail.properties.mail.smtp.connectiontimeout=15000
spring.mail.properties.mail.smtp.socketFactory.fallback=true
spring.mail.properties.mail.mail.debug=true

spring.config.import=sensitive.properties

Next add a spring.config.import statement to import sensitive.properties file, then provide the alternate file.

// sensitive.properties

spring.mail.username=my-user@mydomain.com
spring.mail.password=my-secure-password
prototype.o365.email.from=webmaster@mydomain.com
prototype.o365.email.to=destination-user@your-domain.com

Finally, to prevent sensitive.properties from finding its way to GitHub, be sure to update the .gitignore file to exclude it.

# .gitignore

### SENSITIVE PROPERTIES ###
sensitive*.properties

Send Email using Spring Boot

The final step is to craft and send your email. Use a SimpleMailMessage to send a plaintext email:

SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo(emailTo);
msg.setFrom(emailFrom);
msg.setSubject("plain email at " + new Date().toString());
msg.setText("Hello World\n" + new Date().toString());

try
{
    javaMailSender.send(msg);
}
catch(MailException e)
{
    log.error("error sending plaintext email", e);
}

Use a MIME message to send HTML email content or include attachments:

MimeMessage msg = javaMailSender.createMimeMessage();
try
{
    MimeMessageHelper helper = 
        new MimeMessageHelper(msg, true);

    helper.setTo(emailTo);
    helper.setFrom(emailFrom);
    helper.setSubject("html email at " + new Date().toString());
    helper.setText("<h1>check attachment for the logo</h1>", true);

    helper.addAttachment("logo.png", 
        new ClassPathResource("logo.png"));
}
catch(MessagingException e)
{
    log.error("error preparing email", e);
}

try
{
    javaMailSender.send(msg);
}
catch(MailException e)
{
    log.error("error sending mime email", e);
}

Source Code

This article’s full source code is available on GitHub.