Reputation: 609
I am using the Java mail API for e-mailing. I have to e-mail a message that contains both inline images specified by HTML's <img>
tag and some attached files.
What content type I should use for MimeMultipart
that contains the parts for inline images and attachment files?
MimeMultipart multipartInline = new MimeMultipart(?);
Upvotes: 4
Views: 4733
Reputation: 29961
There's three different types of multipart content to consider here:
You can nest these different types in all sorts of interesting ways.
To answer the original question, you want a message with this structure:
main message
multipart/mixed
multipart/related
text/html - main html content
image/jpg - an image referenced by the html
application/pdf - or whatever, for the first attachment
The html part will want to reference the image part using a "cid:" URL reference, and the image part will need a corresponding Content-ID header. RFC2387 has more details. You can probably find some examples by searching the JavaMail forum.
Upvotes: 5
Reputation: 13841
You must use one or two headers for each attach:
If it's a normal attach:
If it's an inline attach (image for your mail)
This is extracted for a small sending program I've programmed some time ago:
bodyPart
is a MimeBodyPart
.
bodyPart.setHeader("Content-Disposition", disp + "; filename=" + encodedFileName);
bodyPart.setHeader("Content-Transfer-Encoding", "base64");
if (att.getContextId() != null && att.getContextId().length() > 0)
bodyPart.setHeader("Content-ID", "<" + att.getContextId() + ">");
In it: disp has inline
or attachment
, and att.getContextId()
has some arbitrary ID for the inlined attach.
My recipe for an HTML mail
message has via .setContent(...)
mainMultipart is a MimeMultiPart("alternative")
and has via .addBodyPart(...)
textBodyPart is a MimeBodyPart with content-type "text/plain"
relatedMultipart is a MimeMultipart("related")
and has via .addBodyPart(...)
htmlBodyPart "text/html; charset=utf-8"
INLINED-ATTACH1
INLINED-ATTACH2
NORMAL-ATTACH1
NORMAL-ATTACH2
Upvotes: 0