Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Java cannot send email with html with images

I am trying to send email with html which has two images. The two images are sent from the AngularJS client side as base64 strings and looks like:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAALuCAYAAAA9jTxNAAAgAElEQ

Note that I have truncated the base64 string as its too long.

String temp = baseString.split(",")[1];
byte[] tile = DatatypeConverter.parseBase64Binary(temp);

BodyPart messageBodyPart = new MimeBodyPart();
InputStream inputStream = new ByteArrayInputStream(tile);
DataHandler dataHandler = new DataHandler(new InputStreamDataSource(inputStream));
messageBodyPart.setDataHandler(dataHandler);
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);

The InputStreamDataSource:

public class InputStreamDataSource implements DataSource {

    private InputStream inputStream;

    public InputStreamDataSource(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public OutputStream getOutputStream() throws IOException {
        throw new UnsupportedOperationException("Not implemented");
    }

    public String getContentType() {
        return "*/*";
    }

    public String getName() {
        return "InputStreamDataSource";
    }
}

The image does not show up in the mail.

But it works perfectly fine when I use a FileDataSource instead of base64 string:

    DataSource fds = new FileDataSource("D:\\Projects\\Extras\\sofa1.png");
    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.setHeader("Content-ID", "<image>");
    multipart.addBodyPart(messageBodyPart);

This works fine and shows the images.

Someone please help me fix this.

Upvotes: 1

Views: 1184

Answers (2)

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

For anyone else having this issue. I have fixed it using @Bill Shannon's answer. This will set the image src using the base64 string in the html email.

String temp = base64String.split(",")[1];
byte[] tile = DatatypeConverter.parseBase64Binary(temp);
BodyPart messageBodyPart = new MimeBodyPart();
DataHandler dataHandler = new DataHandler(new ByteArrayDataSource(tile, "image/png"));
messageBodyPart.setDataHandler(dataHandler);
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);

The html string is something like:

"<img style=\"height: 100px; width: 100px;\" src=\"cid:image\" alt=\"Tile\" title=\"Tile\">\r\n"

Upvotes: 0

Bill Shannon
Bill Shannon

Reputation: 29971

Use ByteArrayDataSource instead of your own InputStreamDataSource.

Your code only shows one image; hopefully you're using different Content-IDs for each image.

Your code also doesn't should how the html content is referencing the images; hopefully it's using the correct "cid:" URL.

The JavaMail FAQ has more information on how to send messages that include images.

Upvotes: 1

Related Questions