jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

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

Comment on this FAQ entry

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



Reply to this answer/comment  Help  
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



Reply to this answer/comment  Help  
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



Reply to this answer/comment  Help  
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



Reply to this answer/comment  Help  
Re: About sending Multipart text and html mail.
Kevin Bridges, Oct 2, 2003  [replies:3]

I found that the above was useless for me as well. Here is what I needed to do to send a multi-part text/html email. You may need to edit the line:
props.put("mail.smtp.host", "localhost");

You will need to edit the from and to emails.

// Create the message to send
   Properties props = new Properties();
   props.put("mail.smtp.host", "localhost");
   Session session = Session.getInstance(props,null);
   MimeMessage message = new MimeMessage(session);

// Create the email addresses involved
   InternetAddress from = new InternetAddress("from@from.com");
   InternetAddress to = new InternetAddress("to@to.com");

// Fill in header
   message.setSubject("I am a multipart text/html email" );
   message.setFrom(from);
   message.addRecipient(Message.RecipientType.TO, to);

// Create a multi-part to combine the parts
   Multipart multipart = new MimeMultipart();

// Create your text message part
   BodyPart messageBodyPart = new MimeBodyPart();
   messageBodyPart.setText("Here is your plain text message");

// Add the text part to the multipart
   multipart.addBodyPart(messageBodyPart);

// Create the html part
   messageBodyPart = new MimeBodyPart();
   String htmlText = "<H1>I am the html part</H1>";
   messageBodyPart.setContent(htmlText, "text/html");

// Add html part to multi part
   multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message
   message.setContent(multipart);

// Send message
   Transport.send(message);


Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment  Help  
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



Reply to this answer/comment  Help  
Re[3]: About sending Multipart text and html mail.
Asish Law, Mar 9, 2004  [replies:1]
I am having some problems getting "alternative" to work. Here is the section of my code that creates the content (no attachments):

      MimeMessage message = new MimeMessage(session);

      // Create a multi-part to combine the parts
      MimeMultipart multipart = new MimeMultipart("alternative");

      // Create text message part
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(textBody);
      multipart.addBodyPart(messageBodyPart);

      // Create html part
      messageBodyPart = new MimeBodyPart();
      messageBodyPart.setContent(htmlBody, "text/html");
      multipart.addBodyPart(messageBodyPart);

      // Associate multi-part with message
      message.setContent(multipart);
      message.saveChanges();

The above works great for browsers/servers that support html content. Internet services that don't support the used character set give the following error. I've seen postings mentioning this error, and have tried to follow the suggestions. Not sure what I am missing in the code above. Also, if I change "alternative" to "mixed", both (html and text) browsers receive text email with html as an attachment (as pointed out in an earlier message in this thread). Thanks for any help that you can provide.

This message uses a character set that is not supported by the Internet Service. To view the original message content, open the attached message. If the text doesn't display correctly, save the attachment to disk, and then open it using a viewer that can display the original character set. <<message.txt>>

Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment  Help  

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



Reply to this answer/comment  Help  


Ask A Question



 
Related Links

JavaMail FAQ

JavaMail Forum

JavaMail home

JavaBeans Activation Framework

JavaMail API Documentation

Activation API Documentation

JavaMail Tutorial

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers