How to send an email with inline images and regular attachments in JavaMail?
I would like to know how to create a multi-page SMTP message in the correct order so that it displays correctly on the iPhone email client (displaying it correctly in GMail).
I am using Javamail to create an email containing the following parts:
- Body part with content type "text / html; UTF-8"
- Nested attachment.
- Attaching files
I am sending mail via GMail SMTP (over SSL) and mail is sent and displayed correctly using the GMail account, however the mail is not showing correctly on the iPhone mail client. On the iPhone email client, the image appears in front of the "Before Image" text when it should be displayed later. There is a question mark icon after the text "In front of the image" (I assume that means it could not find the reference CID). I am not sure if this is a limitation of the iPhone mail client or a bug in my send mail code (I strongly assume the latter).
I think maybe the headings on my parts might be wrong or maybe I am supplying plurals in the wrong order. I am including the text of the received mail as gmail output (which displays the corc file
Message-ID: <3977333.1.1274154021787.JavaMail.Chris@smtp.gmail.com>
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: <20100518124021763_368238_0>
iVBORw0K ----- TRIMMED FOR CONCISENESS
6p1VVy4alAAAAABJRU5ErkJggg==
------=_Part_0_20870565.1274154021755
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
<html><head><title>Employees Favourite Foods</title>
<style>
body { font: normal 8pt arial; }
th { font: bold 8pt arial; white-space: nowrap; }
td { font: normal 8pt arial; white-space: nowrap; }
</style></head><body>
Before Image<br><img src="cid:20100518124021763_368238_0">
After Image<br><table border="0">
<tr>
<th colspan="4">Employees Favourite Foods</th>
</tr>
<tr>
<th align="left">Name</th><th align="left">Age</th><th align="left">Tel.No</th><th align="left">Fav.Food</th>
</tr>
<tr style="background-color:#e0e0e0">
<td>Chris</td><td>34</td><td>555-123-4567</td><td>Pancakes</td>
</tr>
</table></body></html>
------=_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--
Even if you cannot help me on this, I would appreciate it if anyone on the forum could send me (non-personal) mail that includes inline images (but not external hyperlinks). I just need to find a working sample, then I can get past that.
Thanks,
Chris.
a source to share
I don't see Content-Disposition in the base64 encoded part of the image. You should probably set this to a string. You can even specify the filename here, and there are more options (check them out).
Example:
Content-Disposition: inline; filename="inlineimage1.gif"
It seems a bit tricky that you are setting your content type to application / octet stream, you can just set it to the correct format.
Example:
Content-Type: image/gif; name="inlineimage1.gif"
a source to share
If anyone stumbles upon this, this is how multiparty should be composed:
-
multipart / mixed (will contain text and attachments)
-
multipart / alternative (will contain text and HTML)
- text / plain
- multipart / related (HTML + inline images)
- text / html (Html content, images referenced as src = "cid: xxx"
- image1 (Content-Id: xxx)
- image2
- ...
- attachment 1
- Appendix 2
- ...
-
a source to share