JavaMail API - SPLessons

JavaMail Send Inline Image

Chapter 11

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Send Inline Image

JavaMail Send Inline Image

shape Description

JavaMail Send Inline Image, As everyone know that it is very easy to insert an image in HTML code by using image tags like that through the Java mail API is also an interesting task. Here developer is not using any particular server only through the SMTP server image will be sent to the recipients. Here message body and ContenetType need to be set in the code why because the developer is using HTML code to send an image. Following is an example which describes more regarding how send an image to others.

shape Example

Following is an example which describes more about an JavaMail Send Inline Image.

package firstmail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class inlineimages {
   public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "";
      // Sender's email ID needs to be mentioned
      String from = "";
      final String username = "";//change accordingly
      final String password = "splesson123";//change accordingly
      // Assuming you are sending email through
      String host = "";
      Properties props = new Properties();  
      props.put("", "");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);
         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));
         // Set To: header field of the header.
         // Set Subject: header field
         message.setSubject("SPlessons image");
         // This mail has 2 part, the BODY and the embedded image
         MimeMultipart multipart = new MimeMultipart("related");
         // first part (the html)
         BodyPart messageBodyPart = new MimeBodyPart();
         String htmlText = "<H1>Hey man find the below image</H1><img src=\"cid:image\">";
         messageBodyPart.setContent(htmlText, "text/html");
         // add it
         // second part (the image)
         messageBodyPart = new MimeBodyPart();
         DataSource fds = new FileDataSource(
         messageBodyPart.setDataHandler(new DataHandler(fds));
         messageBodyPart.setHeader("Content-ID", "<image>");
         // add image to the multipart
         // put everything together
         // Send message
         System.out.println("Sent message successfully....");
      } catch (MessagingException e) {
         throw new RuntimeException(e);

When compile the program output will be as follows in the console.

Sent message successfully....

Now check the mail.

shape Programming

  • JavaMail Send Inline Image – MimeBodyPart(); needs to be assigned.
  • JavaMail Send Inline Image – Here program executed with the SMTP server not with any particular server.