Multipart (html) email

T

Tim Slattery

I'm trying to figure out how to send a multipart email, one text part
and equivalent HTML part. I suppose it should be
multipart/alternative. This is what I've done:

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(returnAddress, returnName));
msg.setRecipient(Message.RecipientType.TO,
newInternetAddress(rb.getTo()));

Multipart mp = new MimeMultipart("alternative");

// First part: plain text
MimeBodyPart mbp = new MimeBodyPart();
/* stripTags remove HTML tags from my text */
mbp.setText(stripTags(msgBody), "UTF-8", "plain");
mp.addBodyPart(mbp);

MimeBodyPart mbhtml = new MimeBodyPart();
mbhtml.setText(msgBody, "UTF8", "html");
mp.addBodyPart(mbhtml);
msg.setContent(mp);

Transport.send(msg);

And I get IO Exception, no content. I see a method in the javadoc for
MimeBodyPart: setText(text, charset, subtype), all three arguments
Strings. But the compiler says there is no such method.

So how the heck do I put content into this message?
 
L

Lothar Kimmeringer

Tim said:
I'm trying to figure out how to send a multipart email, one text part
and equivalent HTML part. I suppose it should be
multipart/alternative. This is what I've done: [...]

And I get IO Exception, no content. I see a method in the javadoc for
MimeBodyPart: setText(text, charset, subtype), all three arguments
Strings. But the compiler says there is no such method.

So how the heck do I put content into this message?

I saved time by not looking into my sources where I was doing that
for myself. Instead I was firing up my browser and were going to
http://www.google.de/search?hl=de&q=javamail+create+multipart/alternative&btnG=Google-Suche&meta=
The first result is a discussion in the SUN-forum where a couple
of examples are shown. At least one should work.

In short. Instead of setText you should use setContent and the
Content-Types are not only "text" and "html" but "text/plain"
and "text/html".


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
A

Arne Vajhøj

Tim said:
I'm trying to figure out how to send a multipart email, one text part
and equivalent HTML part. I suppose it should be
multipart/alternative. This is what I've done:

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(returnAddress, returnName));
msg.setRecipient(Message.RecipientType.TO,
newInternetAddress(rb.getTo()));

Multipart mp = new MimeMultipart("alternative");

// First part: plain text
MimeBodyPart mbp = new MimeBodyPart();
/* stripTags remove HTML tags from my text */
> mbp.setText(stripTags(msgBody), "UTF-8", "plain");

mbp.setText(stripTags(msgBody), "UTF-8");
mp.addBodyPart(mbp);

MimeBodyPart mbhtml = new MimeBodyPart();
mbhtml.setText(msgBody, "UTF8", "html");

mbhtml.setContent(msgBody, "text/html; charset=UTF-8");
mp.addBodyPart(mbhtml);
msg.setContent(mp);

Transport.send(msg);

And I get IO Exception, no content. I see a method in the javadoc for
MimeBodyPart: setText(text, charset, subtype), all three arguments
Strings. But the compiler says there is no such method.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top