Connect Spring Mail with OCI Email Delivery Service

Anders Swanson
3 min readJul 30, 2024

--

Spring Cloud Oracle seamlessly integrates Oracle Cloud Infrastructure (OCI) Email Delivery Service + Spring Mail APIs, allowing you to easily send email from Spring Boot apps using well-known SMTP configuration options.

In this article, we’ll set up a Spring Boot app that uses Spring Cloud Oracle and OCI Email Delivery to send mail.

Add Spring Cloud Oracle Dependencies

If you’re using Maven, add the Spring Cloud OCI Starter Email dependency to your project. At time of writing, the latest version is 1.2.0.

<dependency>
<groupId>com.oracle.cloud.spring</groupId>
<artifactId>spring-cloud-oci-starter-email</artifactId>
<version>1.2.0</version>
</dependency>

If you’re using Gradle, add the Spring Cloud OCI Starter Email dependency like so.

implementation 'com.oracle.cloud.spring:spring-cloud-oci-starter-email:1.2.0'

Setup your OCI account for Email Delivery

To use OCI Email Delivery, we’ll need to run through a few one-time configuration steps in your OCI account.

  1. Create one or more approved senders in your account— These are the “from” email addresses that your app will use.
  2. Get the OCI Email Delivery SMTP host and port for your OCI account and region — This is the endpoint Spring Mail will invoke for SMTP sessions.
  3. Create SMTP credentials — We’ll use these credentials to authenticate with the OCI Email Delivery SMTP host.

Save your SMTP credentials somewhere secure (like OCI Vault) so they can be injected into your app as Spring properties.

Configure your Spring Boot app for OCI Email Delivery

Using the values from the previous section, create an application.yaml similar to the following in your Spring Boot application:

spring:
mail:
host: ${OCI_SMTP_HOST}
username: ${OCI_SMTP_USERNAME}
password: ${OCI_SMTP_PASSWORD}
# Port 25 or 587 may be used, 587 is the default.
port: 587

Write a simple Service to send email

We’ll write a simple Spring Boot Service that injects Spring Cloud Oracle Email mail sender beans, giving an example of how to use those beans to send messages.

Note that two separate mail sender beans are available in the application context:

  • The MailSender bean uses Spring APIs for simple messages.
  • The JavaMailSender bean exposes the jakarta.mail API for complex messages, including those with attachments.
import java.io.File;

import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.activation.FileDataSource;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
private final JavaMailSender javaMailSender;
private final MailSender mailSender;

// Use the ociJavaMailSender or ociMailSender qualifiers to
// inject mail sender beans configured to use OCI Email Delivery.
public EmailService(
@Qualifier("ociJavaMailSender") JavaMailSender javaMailSender,
@Qualifier("ociMailSender") MailSender mailSender
) {
this.javaMailSender = javaMailSender;
this.mailSender = mailSender;
}

public void sendWithAttachment(
String from,
String to,
String subject,
String text,
File fileAttachment
) throws MessagingException {
// Create a new java mail message
MimeMessage message = javaMailSender.createMimeMessage();
Multipart multipartContent = new MimeMultipart();
MimeBodyPart textContent = new MimeBodyPart();
textContent.setContent(text, "text/html");
multipartContent.addBodyPart(textContent);
message.setFrom(from);
message.setSubject(subject);
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to)
);

// Attach a file to the message
MimeBodyPart attachmentContent = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
attachmentContent.setDataHandler(new DataHandler(source));
attachmentContent.setFileName(fileAttachment.getName());
multipartContent.addBodyPart(attachmentContent);
message.setContent(multipartContent);

// Send the message using OCI Email Delivery
javaMailSender.send(message);
}

public void sendSimpleMail(
String from,
String to,
String subject,
String text
) {
// Send a simple message using OCI Email Delivery
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(text);
mailSender.send(message);
}
}

Use the ociJavaMailSender @Qualifier for the JavaMailSender bean, and the ociMailSender @Qualifier for the MailSender bean to inject mail senders configured with OCI Email Delivery.

--

--

Anders Swanson

I'm a Developer Evangelist for Oracle Database who is passionate about creating content and tools to help developers succeed.