1

I need to send an image as part of email body using java :-

The Steps I followed is as below

  • Take an image and encode it using org.apache.commons.codec.binary.Base64 and use it in html as image src tag.

  • Decode the encoded image and send using the mailer API.

The Issue I have facing that some image is passing through the mailer API and visible in email body and some are not.

Is it the size of image which is creating the issue ?

Thanks & Regards Kundan Saini

public static void main(String[] args) {

     String htmlContent = readFile("c:\\temp\\imagenotsending.html");


     String encodeHtmlContent = encodeStringToBase64(htmlContent);
     System.out.println(encodeHtmlContent);

     String decodeHtmlBody = decodeStringToBase64(encodeHtmlContent);
     sendMailNew(decodeHtmlBody);
 }  

 public static void sendMailNew(String body) {
    String to = "to address";

    // Sender's email ID needs to be mentioned
    String from = "from address here";

    // Assuming you are sending email from localhost
    String host = "host here";

    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty("host", host);

    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        message.setContent(body, "text/html; charset=utf-8");

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                to));

        // Set Subject: header field
        message.setSubject("This is the Subject Line!");

        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }
}


public static String encodeStringToBase64(String aString) {
    byte[] encodedBytes = Base64.encodeBase64(aString.getBytes());
    return new String(encodedBytes);
}

public static String decodeStringToBase64(String aString) {
    byte[] decodedBytes = Base64.decodeBase64(aString.getBytes());
    return new String(decodedBytes);
}

Html Files as below :-

<html>

<head>
    <title>Test</title>
</head>

<body>
    
     <img src="data:image/png;base64,iVBORw0KGg.......rkJggg==">

                   
</body>

</html>

Base 64 encoding of image below not sending Base 64 encoding of image below is sending

Kundan Saini
  • 87
  • 2
  • 6
  • 15
  • Edit your question and include your code. You almost certainly should not be using a third party library for base64 encoding; [MimeBodyPart](https://docs.oracle.com/javaee/7/api/javax/mail/internet/MimeBodyPart.html) already supports attaching files in base64. – VGR Nov 02 '17 at 15:46
  • Are you sending the image as a separate body part? Or are you embedding the encoded image in the html content or your main body part? If the former, you definitely do not need to encode it yourself, and you'll want to be sure to use a multipart/related to hold the main html content and the image body part, as described in the [JavaMail FAQ](https://javaee.github.io/javamail/FAQ#sendmpr). – Bill Shannon Nov 02 '17 at 21:32
  • @VGR I have edited the question and put the code as well, I am using Apache base 64 library to encode and decode – Kundan Saini Nov 03 '17 at 10:31
  • @BillShannon No I am not sending it as a body part but as an encoded image in HTML as mentioned in the code Multipart will send the image as a attachment I guess – Kundan Saini Nov 03 '17 at 10:31
  • Now that I can see your HTML file, I see that the image is already encoded in base64, within the HTML file. Do not attempt to encode the entire HTML file in base64. In fact, you can just replace your call to message.setContent with `message.setDataHandler(new DataHandler(new FileDataSource("c:\\temp\\imagenotsending.html")));`. – VGR Nov 03 '17 at 11:04
  • @VGR I have done it because of the requirement, I will get html file from some other system in base64 encrypted format which my system has to extract from a string and decrypt it. the decypted html (which has base 64 encoded image) now needs to be attached as email body to send the email. but the problem I am facing the html is rendering fine but the encoded image inside the html is not rendering in email body. I have tried on outlook, outlook 365, gmail – Kundan Saini Nov 03 '17 at 12:35
  • @VGR so it seems there is only one option instead of sending the image as email body inline image I should try to send an attachment for the image – Kundan Saini Nov 06 '17 at 08:56

1 Answers1

2

So you are trying to send base64 image inside mail body. Problem may be connected with the fact that this is not supported by some mail agents.

You can see it here: Send a base64 image in HTML email

So basicly the better way to send image in mail body is using cid and image as an attachment(also doesn't works everywhere).

There is a good article which describes differend ways of adding image to mail and their pros and cons: https://sendgrid.com/blog/embedding-images-emails-facts/

Ruslan Akhundov
  • 2,178
  • 4
  • 20
  • 38
  • @Ruslan Akhundov Thanks for links I have gone through it so do you mean its the issue with outlook or gmail that they are not allowing the image but the question I am having why they are allowing other images I have put both the images, code and html code as well what I am using – Kundan Saini Nov 03 '17 at 10:33