title: How do I send an email with embedded images and regular attachments in JavaMail?
body: <p>I'd like to know how to build an SMTP multipart message in the correct order so that it will render correctly on the iPhone mail client (rendering correctly in GMail).</p>
  
  <p>I'm using Javamail to build up an email containing the following parts:</p>
  
  <ul>
  <li>A body part with content type "text/html; UTF-8"</li>
  <li>An embedded image attachment.</li>
  <li>A file attachment</li>
  </ul>
  
  <p>I am sending the mail via GMail SMTP (via SSL) and the mail is sent and rendered correctly using a GMail account, however, the mail does not render correctly on the iPhone mail client. On the iPhone mail client, the image is rendered before the "Before Image" text when it should be rendered afterwards. After the "Before Image" text there is an icon with a question mark (I assume it means it couldn't find the referenced CID). I'm not sure if this is a limitation of the iPhone mail client or a bug in my mail sending code (I strongly assume the latter).</p>
  
  <p>I think that perhaps the headers on my parts might by incorrect or perhaps I am providing the multiparts in the wrong order. I include the text of the received mail as output by gmail (which renders the file correc</p>
  
  <pre><code>Message-ID: &lt;3977333.1.1274154021787.JavaMail.Chris@smtp.gmail.com&gt;
  Subject: =?UTF-8?Q?Test_from_=E3=82=AF=E3=83=AA=E3=82=B9?=
  MIME-Version: 1.0
  Content-Type: multipart/mixed; 
      boundary="----=_Part_0_20870565.1274154021755"
  
  ------=_Part_0_20870565.1274154021755
  Content-Type: application/octet-stream
  Content-Transfer-Encoding: base64
  Content-ID: &lt;20100518124021763_368238_0&gt;
  
  iVBORw0K ----- TRIMMED FOR CONCISENESS
  6p1VVy4alAAAAABJRU5ErkJggg==
  ------=_Part_0_20870565.1274154021755
  Content-Type: text/html; charset=UTF-8
  Content-Transfer-Encoding: 7bit
  
  &lt;html&gt;&lt;head&gt;&lt;title&gt;Employees Favourite Foods&lt;/title&gt;
  &lt;style&gt;
          body { font: normal 8pt arial; }
          th   { font: bold   8pt arial; white-space: nowrap; }
          td   { font: normal 8pt arial; white-space: nowrap; }
  &lt;/style&gt;&lt;/head&gt;&lt;body&gt;
  
          Before Image&lt;br&gt;&lt;img src="cid:20100518124021763_368238_0"&gt;
          After Image&lt;br&gt;&lt;table border="0"&gt;
  &lt;tr&gt;
  &lt;th colspan="4"&gt;Employees Favourite Foods&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;th align="left"&gt;Name&lt;/th&gt;&lt;th align="left"&gt;Age&lt;/th&gt;&lt;th align="left"&gt;Tel.No&lt;/th&gt;&lt;th align="left"&gt;Fav.Food&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr style="background-color:#e0e0e0"&gt;
  &lt;td&gt;Chris&lt;/td&gt;&lt;td&gt;34&lt;/td&gt;&lt;td&gt;555-123-4567&lt;/td&gt;&lt;td&gt;Pancakes&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;&lt;/body&gt;&lt;/html&gt;
  ------=_Part_0_20870565.1274154021755
  Content-Type: text/plain; charset=us-ascii; name=textfile.txt
  Content-Transfer-Encoding: 7bit
  Content-Disposition: attachment; filename=textfile.txt
  
  This is a textfile with numbers counting from one to ten beneath this line:
  one
  two
  three
  four
  five
  six
  seven
  eight
  nine
  ten(no trailing carriage return)
  ------=_Part_0_20870565.1274154021755--
  </code></pre>
  
  <p>Even if you can't assist me with this, I would appreciate it if any members of the forum could forward me a (non-personal) mail that includes inline images (not external hyperlinked images though). I just need to find a working sample then I can move past this.</p>
  
  <p>Thanks,</p>
  
  <p>Chris.</p>