title: Sending e-mail with inline images using JavaMail API
body: <p>I am using the following code to send a email along with the image and it is working perfectly. When I send the image, it is getting displayed in the body instead of getting attached in the e-mail. So, that I can easily download that image from the attachment.</p>
  
  <p>Is there any way to add image in the attachment image in the e-mail instead of the image present in body?</p>
  
  <pre><code>package com.tutorialspoint;
  
  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 SendInlineImagesInEmail
  {
      public static void main(String[] args)
      {
          // Recipient's email ID needs to be mentioned.
          String to = "destinationemail@gmail.com";
  
          // Sender's email ID needs to be mentioned
          String from = "fromemail@gmail.com";
          final String username = "manishaspatil"; //change accordingly
          final String password = "******"; //change accordingly
  
          // Assuming you are sending email through relay.jangosmtp.net
          String host = "relay.jangosmtp.net";
  
          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "25");
  
          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.
              message.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse(to));
  
              // Set Subject: header field
              message.setSubject("Testing Subject");
  
              // 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 = "&lt;H1&gt;Hello&lt;/H1&gt;&lt;img src=\"cid:image\"&gt;";
              messageBodyPart.setContent(htmlText, "text/html");
              // add it
              multipart.addBodyPart(messageBodyPart);
  
              // second part (the image)
              messageBodyPart = new MimeBodyPart();
              DataSource fds = new FileDataSource(
                  "/home/manisha/javamail-mini-logo.png");
  
              messageBodyPart.setDataHandler(new DataHandler(fds));
              messageBodyPart.setHeader("Content-ID", "&lt;image&gt;");
  
              // add image to the multipart
              multipart.addBodyPart(messageBodyPart);
  
              // put everything together
              message.setContent(multipart);
              // Send message
              Transport.send(message);
  
              System.out.println("Sent message successfully....");
  
          } catch (MessagingException e) {
              throw new RuntimeException(e);
          }
      }
  }
  </code></pre>