Send email with inline attachment or resource via Spring

This tutorial will show you how to send a basic mail via Spring framework’s email support. We will attach a resource (an image) as an inline attachment in the email body.

The Spring Framework provides a helpful utility library class that comes in pretty handy when dealing with JavaMail messages is the org.springframework.mail.javamail.MimeMessageHelper class, which shields you from having to use the verbose JavaMail API.

Using the MimeMessageHelper it is pretty easy to create a MimeMessage. Multipart email messages allow for both attachments and inline resources.

Examples of inline resources would be images or a stylesheet you want to use in your message, but that you don’t want displayed as an attachment. Inline resources are added to the mime message using the specified Content-ID (file.getName() in the below example).

The order in which you are adding the text and the resource are very important. Be sure to first add the text and after that the resources. If you are doing it the other way around, it won’t work!

The following example shows you how to use the MimeMessageHelper to send an email along with an inline image.


Eclipse 2019-12, Java at least 1.8, Gradle 6.4.1, Maven 3.6.3, Spring Boot Mail Starter 2.3.1, Java mail API 1.6.2

Project Setup

Create either gradle or maven based project in Eclipse. The name of the project is spring-email-inline-attachment.

If you are creating gradle based project then you can use below build.gradle script:

buildscript {
	ext {
		springBootVersion = '2.3.1.RELEASE'
    repositories {
    dependencies {

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

If you are creating maven based project then you can use below pom.xml file:

<project xmlns=""





					<source>at least 8</source>
					<target>at least 8</target>

Email Configuration

We will create an email configuration class where we will configure SMTP (Simple Mail Transfer Protocol) server details, from email address.


import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

public class EmailConfig {

	public JavaMailSender mailSender() {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

		mailSender.setPassword("gmail password");

		Properties javaMailProperties = new Properties();
		javaMailProperties.put("mail.smtp.auth", true);
		javaMailProperties.put("mail.smtp.starttls.enable", true);


		return mailSender;


We need to issue STARTTLS command otherwise you will get the error message similar to the following:

#smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first. p7sm24605501pfn.14 - gsmtp', '')

You will get below error if your security level is high:

#smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 x10sm26098036pfn.36 - gsmtp')

Therefore you need to lower your Gmail’s security settings.

Email Sender

Email sender class just does the right job for sending the email to the intended recipient.

We have checked whether users want to send HTML message or text message in the email.

We have specified second parameter of MimeMessageHelper class’s constructor’s with true value to indicate that we want to send multipart form data.

We are adding the inline attachment using addInline() method.



import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

public class EmailSender {

	private JavaMailSender mailSender;

	public void sendEmailAttachment(final String subject, final String message, final String fromEmailAddress,
			final String toEmailAddresses, final boolean isHtmlMail, final File attachment) {
		try {
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

			if (isHtmlMail) {
				helper.setText("<html><body>" + message + "</html></body>", true);
			} else {

			// attach the file into email body
			FileSystemResource file = new FileSystemResource(attachment);
			helper.addInline(attachment.getName(), file);


			System.out.println("Email sending complete.");
		} catch (Exception e) {


Main Class

A class is having main method with @SpringBootApplication annotation is enough to start up the Spring Boot application.

In this class we specify the subject, message, true flag for an HTML email and the recipient to send the email.

We have also attached an image file sample.jpg to attach as an inline resource in the email body.



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class EmailInlineAttachmentApp implements CommandLineRunner {

	private EmailSender emailSender;

	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {
		emailSender.sendEmailAttachment("Email Inline Attachment", "Email Inline Attachment using MimeMessageHelper",
				"", "", true, new File("C:/jee_workspace/sample.jpg"));


Testing the Application

Now executing the main class will send the email to the intended recipient and you will see the below output in console:

Email sending complete.

Check the inbox you will get a message. If you do not find the message in inbox then check the Spam folder.

I have sent email to myself, so I am seeing me as a from email address.

Email in inbox:

email with inline attachment using spring

Email details:

email with inline attachment using spring

Source Code


Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *