title: Sending an HTML e-mail with an inline image with JavaMail - Slow image loading?
body: <p>When I send an HTML e-mail using JavaMail, and include an inline image in the HTML, the image takes 2-3 seconds to load when read in either Gmail or Yahoo.</p>
  
  <p>The image that I am using is a small .png that is about 200 bytes in size.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import java.io.File;
  import java.util.Properties;
  
  import javax.activation.DataHandler;
  import javax.activation.DataSource;
  import javax.activation.FileDataSource;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Multipart;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.AddressException;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeBodyPart;
  import javax.mail.internet.MimeMessage;
  import javax.mail.internet.MimeMultipart;
  
  
  public class EmailTest 
  {   
      static String from = "Your_Gmail_Account_Name";  // (e.g., "name" if your account is "name@gmail.com"
      static String password = "Your_Gmail_Password";    
      static String to = "Send_Here@gmail.com";
      static String subject = "test";
      static String body = "&lt;h1&gt;The image in this e-mail is slow to load.&lt;/h1&gt;&lt;img src=\"cid:my-image\"&gt;"; 
      static String host = "smtp.gmail.com";
  
      public static void main(String[] args)
      {
          Properties properties = System.getProperties();
  
          properties.put("mail.smtp.starttls.enable", "true");  
          properties.put("mail.smtp.host", host);
          properties.put("mail.smtp.user", from);
          properties.put("mail.smtp.password", password);
          properties.put("mail.smtp.port", "587");
          properties.put("mail.smtp.auth", "true");
  
          Session session = Session.getDefaultInstance(properties);
  
          MimeMessage message = new MimeMessage(session);
  
          try 
          {
              message.setFrom(new InternetAddress(from));    
              InternetAddress toAddress = new InternetAddress(to);
              message.addRecipient(Message.RecipientType.TO, toAddress);
              message.setSubject(subject);
  
              Multipart multipart = new MimeMultipart("related");
  
              MimeBodyPart htmlPart = new MimeBodyPart();
              htmlPart.setContent(body, "text/html");
              multipart.addBodyPart(htmlPart);
  
              MimeBodyPart imagePart = new MimeBodyPart();
              DataSource ds = new FileDataSource(new File("src\\icon.png"));
              imagePart.setDataHandler(new DataHandler(ds));
              imagePart.setHeader("Content-ID", "&lt;my-image&gt;");
              multipart.addBodyPart(imagePart);
  
              message.setContent(multipart);
  
              Transport transport = session.getTransport("smtp");
  
              try 
              {
                  transport.connect(host, from, password);
                  transport.sendMessage(message, message.getAllRecipients());
                  transport.close();
  
                  System.out.println("e-mail sent.");
              } 
              catch (MessagingException e) 
              {
                  e.printStackTrace();
              }
          }
          catch (AddressException e) 
          {
              e.printStackTrace();
          }
          catch (MessagingException e) 
          {
              e.printStackTrace();
          }
      } 
  }
  </code></pre>
  
  <p>Does anyone know why the image is so slow to load?</p>
  
  <p>UPDATE:</p>
  
  <p>When reading the e-mail produced by the above code in Thunderbird, the image wouldn't even load, but would only show up as an attachment.</p>
  
  <p>But if I remove this line:</p>
  
  <pre><code>imagePart.setHeader("Content-ID", "&lt;my-image&gt;");
  </code></pre>
  
  <p>and replace it with these two lines:</p>
  
  <pre><code>imagePart.addHeader("Content-ID", "&lt;my-image&gt;");
  imagePart.addHeader("Content-Type", "image/png");
  </code></pre>
  
  <p>then the image actually loads in Thunderbird, and loads instantaneously.</p>
  
  <p>However, the image is still slow to load in both Gmail and Yahoo.</p>