|
Question
|
How do I use JavaMail API to send multipart/alternative mails?
I want to send an html-mail, but if the recipiant dont know how to parse them, they will see a text/plain version.
|
|
Derived from
|
A question posed by Stefan Mellberg
|
|
Topics
|
Java:API:JavaMail:Multipart
|
|
Author
|
Andrea Pompili
|
|
Created
|
Aug 23, 2000
|
Modified
|
Nov 12, 2001
|
|
Answer
You have to build the message using a MimeMultipart object as content.
If you have attachments you have to set the first part of the message with a MimeMultipart object
.
To build a multipart/alternative object with a plain content and an html one follow this code:
String plain_text = ... ;
String html_text = ... ;
MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart text = new MimeBodyPart();
MimeBodyPart html = new MimeBodyPart();
text.setText(plain_text);
html.setContent(html_text, "text/html");
content.addBodyPart(text);
content.addBodyPart(html);
To parse a message with a content multipart/alternative you have to use the MimeMultipart class and then choose from the parts inside the one you prefer according to its content type.
Is this item
helpful? yes no
Previous votes Yes: 3 No: 1
|
|
Comments and alternative answers
 |
Example Code Doesn't Work
David Rosner, Jun 14, 2001 [replies:2]
I tried the following code and the resulting message displayed both the text and the html within the email body. In addition looking at the mail headers it doesn't look like my mail client is recognizing the message as a MimeMultipart.
Thoughts as to why?
MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart text = new MimeBodyPart();
MimeBodyPart html = new MimeBodyPart();
text.setText( this.strBody );
html.setContent(this.strHTMLBody, "text/html");
content.addBodyPart(text);
content.addBodyPart(html);
message.setContent( content );
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
 |
Re: Example Code Doesn't Work
Jin Bal, Nov 5, 2001 [replies:1]
I am having exactly the same problem. What seems to be happening is that the header is not being changed from text/plain by the setContent(Multipart) method, so that when the client reads it it thinks that it is text. I have no solution to this currently and I've been trawling the mail archives for a fix - somebody please help!!
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
 |
 |
Re: Re: Example Code Doesn't Work
david rosner, Nov 5, 2001
Hi,
Don't know why the following works, but it does. It appears that there is either a bug in the JavaMail implementation, or its a typo in the docs. Here is the code that I used to mail HTML and Text multipart emails:
MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart text = new MimeBodyPart();
MimeBodyPart html = new MimeBodyPart();
text.setText( oMail.getStrBody() );
text.setHeader("MIME-Version" , "1.0" );
text.setHeader("Content-Type" , text.getContentType() );
html.setContent(oMail.getStrHTMLBody(), "text/html");
html.setHeader("MIME-Version" , "1.0" );
html.setHeader("Content-Type" , html.getContentType() );
content.addBodyPart(text);
content.addBodyPart(html);
message.setContent( content );
message.setHeader("MIME-Version" , "1.0" );
message.setHeader("Content-Type" , content.getContentType() );
message.setHeader("X-Mailer", "Recommend-It Mailer V2.03c02");
message.setSentDate(new Date());
Is this item
helpful? yes no
Previous votes Yes: 1 No: 1
|
|

|
 |
About sending Multipart text and html mail.
Kevin Russell, Sep 18, 2002 [replies:4]
Is it possible to either post a code example that does 2 things, is clear to understand and actually works. The examples provided do not clearly define what happens where and when.
This looks like something Microsoft would post as an answer to a question.
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
 |
 |
Re[2]: About sending Multipart text and html mail.
Brendan Richards, Feb 23, 2004 [replies:2]
I had just one problem with this code:
It correctly setup two message parts, one text and one html but it gave the main message a mime type of multipart/mixed.
This resulted in it being displayed as a plain text email with the HTML body attached as a HTML file.
I corrected it by setting the main mime type to multipart/alternavive by replacing this line:
Multipart multipart = new MimeMultipart();
to read:
Multipart multipart = new MimeMultipart("alternative");
This then worked as expected with only one of the body parts being displayed on the client.
Is this item
helpful? yes no
Previous votes Yes: 1 No: 0
|
|

|
 |
 |
 |
 |
 |
Re[4]: About sending Multipart text and html mail.
David Green, Aug 9, 2007
This is what worked for me:
Multipart multipart = new MimeMultipart("alternative");
BodyPart plainMessageBodyPart = new MimeBodyPart();
plainMessageBodyPart.setContent(plainTextMessageContent,"text/plain");
multipart.addBodyPart(plainMessageBodyPart);
BodyPart htmlMessageBodyPart = new MimeBodyPart();
htmlMessageBodyPart.setContent(htmlMessageContent, "text/html");
multipart.addBodyPart(htmlMessageBodyPart);
message.setContent(multipart);
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
|
|
 |
|