title: Java mail - attachments && inline images
body: <p>I had a problem solved already this morning:
  <a href="https://stackoverflow.com/questions/8970455/java-mail-sending-multiple-attachments-not-working">Java Mail, sending multiple attachments not working</a></p>
  
  <p>This time I have a slightly more complicated problem: I would like to combine attached files with images.    </p>
  
  <pre><code>import java.io.IOException;
  import java.util.Properties;
  
  import javax.activation.DataHandler;
  import javax.activation.FileDataSource;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  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 MailTest
  {
  
      public static void main(String[] args) throws AddressException, MessagingException, IOException
      {
          String host = "***";
          String from = "***";
          String to = "***";
  
          // Get system properties
          Properties props = System.getProperties();
  
          // Setup mail server
          props.put("mail.smtp.host", host);
  
          // Get session
          Session session = Session.getDefaultInstance(props, null);
  
          // Define message
          MimeMessage message = new MimeMessage(session);
          message.setFrom(new InternetAddress(from));
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          message.setSubject("Hello JavaMail");
  
          // Handle attachment 1
          MimeBodyPart messageBodyPart1 = new MimeBodyPart();
          messageBodyPart1.attachFile("c:/Temp/a.txt");
  
          // Handle attachment 2
          MimeBodyPart messageBodyPart2 = new MimeBodyPart();
          messageBodyPart2.attachFile("c:/Temp/b.txt");
  
          FileDataSource fileDs = new FileDataSource("c:/Temp/gti.jpeg");
          MimeBodyPart imageBodypart = new MimeBodyPart();
          imageBodypart.setDataHandler(new DataHandler(fileDs));
          imageBodypart.setHeader("Content-ID", "&lt;myimg&gt;");
          imageBodypart.setDisposition(MimeBodyPart.INLINE);
  
          // Handle text
          String body = "&lt;html&gt;&lt;body&gt;Elotte&lt;img src=\"cid:myimg\" width=\"600\" height=\"90\" alt=\"myimg\" /&gt;Utana&lt;/body&gt;&lt;/html&gt;";
  
          MimeBodyPart textPart = new MimeBodyPart();
          textPart.setHeader("Content-Type", "text/plain; charset=\"utf-8\"");
          textPart.setContent(body, "text/html; charset=utf-8");
  
          MimeMultipart multipart = new MimeMultipart("mixed");
  
          multipart.addBodyPart(textPart);
          multipart.addBodyPart(imageBodypart);
          multipart.addBodyPart(messageBodyPart1);
          multipart.addBodyPart(messageBodyPart2);
  
          message.setContent(multipart);
  
          // Send message
          Transport.send(message);
      }
  }
  </code></pre>
  
  <p>When I open the email in Gmail everything is fine: I have the two attachments, and the image is displayed in the content of the mail (in the img tag). </p>
  
  <p>The problem is with Thunderbird and with RoundCubic webmail: each one showing like the image is missing and displaying it in the bottom as attachment.</p>
  
  <p>How can I make this work?</p>