How do I send mail with both plain text as well as HTML text so that each mail reader(Client) can choose the format appropriate for it?
I tried following code. but the output was in a mixed format - no html formattting. Also, it was displaying the headers and boundary string.
Any Idea what went wrong?
or could you give me some example using javax.mail?
Session session = Session.getInstance( new Properties (),
null );
Transport trans = session.getTransport("smtp");
MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart htmlBody = new MimeBodyPart();
MimeBodyPart textBody = new MimeBodyPart();
String txtmessage = "this is a text based message" +
"is being sent\n\tbye";
textBody.setContent(txtmessage,"text/plain");
textBody.setHeader("Content-Type",
"text/plain; charset=\"iso-8859-1\"" );
htmlBody.setContent(message,"text/html");
htmlBody.setHeader("Content-Type",
"text/html; charset=\"iso-8859-1\"" );
content.addBodyPart(textBody);
content.addBodyPart(htmlBody);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.
setRecipients
(Message.RecipientType.TO,InternetAddress.
parse(to,false));
msg.setSubject(subject);
msg.setContent( content );
msg.setHeader("Content-Transfer-Encoding", "7bit");
msg.setSentDate(new Date());
trans.connect (host, "", "");
trans.sendMessage( msg, InternetAddress.parse( to ))
-
How do I send mail with both plain text as well as HTML ? (0 messages)
- Posted by: sgt pepper
- Posted on: August 21 2002 12:44 EDT