How do I send an email with embedded images and regular attachments in JavaMail?
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).
I'm using Javamail to build up an email containing the following parts:
- A body part with content type "text/html; UTF-8"
- An embedded image attachment.
- A file attachment
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).
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
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 styl开发者_StackOverflow社区e="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 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.
Thanks,
Chris.
Have you read RFC 2822, which defines specification requirements for email? I haven't read through this myself so I'm not sure if it mentions multi-part email, but it might be a good place to start. Apple seems to be pretty good at sticking to standards.
http://www.faqs.org/rfcs/rfc2822.html
I´m not seeing any Content-Disposition in the base64 encoded image part. You should probably set this to inline. You can even include a filename here and there are more options (look them up).
Example:
Content-Disposition: inline; filename="inlineimage1.gif"
It seems a bit dodgy you're setting your content-type to application/octet-stream as well, you can just set this to the correct format.
Example:
Content-Type: image/gif; name="inlineimage1.gif"
If someone stumbles on this - here's how the multiparts should be made up:
multipart/mixed (Will contain text and attachments)
multipart/alternative (Will contain text and HTML)
- text/plain (Plain text)
- multipart/related (HTML + embedded images)
- text/html (Html content, Images referenced as src="cid:xxx"
- image1 (Content-Id: xxx)
- image2
- ...
- attachment 1
- attachment 2
- ...
精彩评论