aloo
aloo

Reputation: 5389

How to nest MimeBodyParts in JavaMail (standard email messages)?

I'm trying to send an html email (with a text alternative) that includes an embedded/inline image using JavaMail.

I wanted to see what a properly formatted message looked like so I sent one using gmail with an inline image and below is the original source. It seems as if they are nesting body parts in the message. How can I replicate this EXACTLY using javamail. I found no way to put a Multipart inside of another Multipart.

MIME-Version: 1.0 Received: by 10.50.75.3 with HTTP; Thu, 23 Feb 2012 20:15:34 -0800 (PST) Date: Thu, 23 Feb 2012 20:15:34 -0800 Delivered-To: [email protected] Message-ID: Subject: c From: Foo Bar To: Foo Bar Content-Type: multipart/related; boundary=e89a8f3b9b051e124104b9ae03fb

--e89a8f3b9b051e124104b9ae03fb Content-Type: multipart/alternative; boundary=e89a8f3b9b051e123604b9ae03fa

--e89a8f3b9b051e123604b9ae03fa Content-Type: text/plain; charset=ISO-8859-1

plain text content goes here

--e89a8f3b9b051e123604b9ae03fa Content-Type: text/html; charset=ISO-8859-1


--e89a8f3b9b051e123604b9ae03fa-- --e89a8f3b9b051e124104b9ae03fb Content-Type: image/png; name="logo.png" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: ii_135ad92205fc1ace

iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAh0lEQVR42u3ZsQnAIBBGYVtHShVI 7ViprASXcw8HMCckEGyTkB98xWsE4WvOwnMhBG/tVrGaSOU0+Q5MVhMtdWAVBlY3HCyW+7nlbhqB TiGAAAECBAhwMqB0AN8ANuUkgQzJvEPy1WP75C5AgAABAgQ4GXATAK4DUP8LOAsD87WGiIJriGj5 AwiiiyDxmymtAAAAAElFTkSuQmCC --e89a8f3b9b051e124104b9ae03fb--

UPDATE:

Using the standard multipart inline image methods (like those linked to by lechlukasz) produces the following email, not quite what I need:

MIME-Version: 1.0 Received: by 10.236.146.106 with SMTP id q70mr1894063yhj.0.1330071158663; Fri, 24 Feb 2012 00:12:38 -0800 (PST) Reply-To: Foo Bar Message-ID: <[email protected]> Date: Fri, 24 Feb 2012 08:12:38 +0000 Subject: please oh please4 From: Foo Bar To: Foo Bar Content-Type: multipart/alternative; boundary=20cf303bfc80f02ce704b9b152d6

--20cf303bfc80f02ce704b9b152d6 Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes Content-Transfer-Encoding: base64

77+9UE5HDQoaDQoAAAANCklIRFIAAAAoAAAAKAgGAAAA77+977+977+9bQAAAO+/vUlEQVR477+9 77+977+977+9Ce+/vSAgDQoQRmFbR0oVSO+/vVjvv73vv70E77+9cw8HMCckEGzvv73vv70ffO+/ vWsE77+9a++/ve+/vXMhBG/vv71W77+977+9SO+/vTTvv70OTFYTLXVgFQZWNxws77+977+977+9 77+9bhrvv71OIe+/vQABAgQIcDLvv710AO+/vQA277+9JO+/vQzvv73vv71D77+977+9Y++/ve+/ vS5A77+9AAECBDgZcBMA77+9A1Dvv70LOAsD77+977+977+977+977+9a++/vWjvv70DCO+/ve+/ vSDvv73vv70p77+9AAAAAElFTkTvv71CYO+/vQ0K --20cf303bfc80f02ce704b9b152d6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

--20cf303bfc80f02ce704b9b152d6--

Upvotes: 4

Views: 1583

Answers (3)

Andreas Covidiot
Andreas Covidiot

Reputation: 4755

in essence, this is what to do:

Message msg = new MimeMessage( session );
msg.setFrom( "from foo" );
msg.setRecipients( Message.RecipientType.TO, "[email protected]" );
msg.setSubject( "subj" );

MimeMultipart altAndAtt = new MimeMultipart( "mixed" );

{ // add alternative plain + html part

    MimeMultipart plainAndHtml = new MimeMultipart( "alternative" );

    MimeBodyPart plain = new MimeBodyPart();
    plain.setContent( plainBody, "text/plain; charset=utf-8" );
    plainAndHtml.addBodyPart( plain );

    MimeBodyPart html = new MimeBodyPart();
    html.setContent( htmlBody, "text/html; charset=utf-8" );
    plainAndHtml.addBodyPart( html );

    MimeBodyPart wrapper = new MimeBodyPart();  // needed for nesting multipart in multipart
    wrapper.setContent( plainAndHtml );
    altAndAtt.addBodyPart( wrapper );
}

{ // add attachment part

    MimeBodyPart attPart = new MimeBodyPart();
    attPart.attachFile( myFilePath );

    altAndAtt.addBodyPart( attPart );
}


msg.setContent( altAndAtt );

Upvotes: 1

Bill Shannon
Bill Shannon

Reputation: 29971

You put a Multipart in another Multipart by creating a MimeBodyPart, setting the content of that to the Multipart, and adding that body part to the first Multipart.

Upvotes: 8

Cjxcz Odjcayrwl
Cjxcz Odjcayrwl

Reputation: 22847

You mean something like that:

http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#IncludingImagesWithHTML

Note that you need also modify your html to specify cid:identifier as image src.

Upvotes: 0

Related Questions