1. Introduction

In this article, we’re going to build a Java program to send an inline image from Amazon SES. We will create a content id and then reference it under email body.

2. Loading Image Resource

First, we need an image resource that will be included in the email body. Let’s add an image resource.

private static URL imagePath = SESInlineImageEmail.class.getResource("/cloudtechpro-logo.png");

In the sample code, we used a resource folder to load resources. You can find this image under project resources folder.

3. Attaching Image

First, we need a MimeBodyPart object to attach the image along with Content-ID header. We will use this content id to reference image in HTML body. In our case content id is ctp.

MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "<ctp>");

Second, we’ll include the image tag with the content id. Let’s create the email body.

private static StringBuffer body = new StringBuffer("<html><h1>Email with inline image</h1>");
static {
    body.append("Image :<br>");
    body.append("<img src=\"cid:ctp\" width=\"50%\" height=\"50%\" /><br>");
    body.append("End of inline email.");

Now’ we’ve created the email body with HTML content.

4. Sending Email

Amazon SES provides SendRawEmail API to send multipart MIME emails. We first need to create a RawMessage to consumed by this API. Let’s create a RawMessage in Java.

Session session = Session.getInstance(new Properties());

// creates a new e-mail message
MimeMessage message = new MimeMessage(session);

// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body.toString(), "text/html");

// creates multi-part
Multipart multipart = new MimeMultipart();

// adds inline image
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "<ctp>");



ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));

First, we have added a subject. Second, we have added HTML content. Next, attached image file. Finally, we have converted MimeMessage to RawMessage object.

Now, we are ready to send a raw email. We will include destination and source emails in the request. Let’s prepare a request and send a raw email using Amazon SES service.

SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage);
SendRawEmailResult result = ses.sendRawEmail(rawEmailRequest);

If everything successful, you will get a message id as a result.


In this article, we’ve seen sending an inline image using Amazon SES service.

As always, sample program can be found over on GitHub.

Sending Inline Image using Amazon SES
Tagged on:             

Satish Pandey

I am an expert Java Spring Angular developer with 10+ years of rich and varied experience in developing end-to-end Web Applications. I maintain this blog and publish articles in my free time to help the community. Email: satish@cloudtechpro.com

Leave a Reply

Notify of