Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Mail text and html with attachment  RSS feed

 
John Hutcheson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a preparator but can't see how to include all three parts. most examples show html and attachments only.
the mail structure should be this(having examined a thunderbird message):

+-----------------------------------------------+
| multipart/related                             |
| +---------------------------+  +------------+ |
| |multipart/alternative      |  | image/gif  | |
| | +-----------+ +---------+ |  |            | |
| | |text/plain | |text/html| |  |            | |
| | +-----------+ +---------+ |  |            | |
| +---------------------------+  +------------+ |
+-----------------------------------------------+


but I don't know how to add the multipart/alternative part to the multipart/related part. Can someone show me how?
here is what I have so far...
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll move this to our Other JSE / JEE APIs forum, since that's the place for JavaMail.

You already have the structure, so all you need to do is translate that to code:
- a Multipart with sub type "related" as the root
- that one will get two BodyParts: one with another Multipart and one for the file
- the other Multipart will have sub type "alternative" and two BodyParts for the text and HTML parts
 
John Hutcheson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:I'll move this to our Other JSE / JEE APIs forum, since that's the place for JavaMail.

You already have the structure, so all you need to do is translate that to code:
- a Multipart with sub type "related" as the root
- that one will get two BodyParts: one with another Multipart and one for the file
- the other Multipart will have sub type "alternative" and two BodyParts for the text and HTML parts


That right. Its the coding I'm having trouble with what API do I need to add a Multipart to as a BodyPart?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. Part has two setContent methods. One takes an Object and String type, the other takes a Multipart. Just use the second one.
 
John Hutcheson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:That's right. Part has two setContent methods. One takes an Object and String type, the other takes a Multipart. Just use the second one.


This is what I intuitively would like to do but see there is a hitch...
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BodyPart also implements Part.

You've already specified the structure in your starting post. All you need to do is paste these together:
- text/plain goes into a BodyPart of its own
- text/html goes into a BodyPart of its own
- both BodyParts go into a Multipart with sub type "alternative"
- image/gif goes into a BodyPart of its own
- the Multipart goes into a BodyPart which is added to a Multipart with sub type "related", together with the GIF BodyPart
- that Multipart will be the content of your message

I've seen you added some code. You'll need to do just one thing: wrap cover into a MimeBodyPart, then add that to content. Oh, and I'd wait with setting the message content until it's fully constructed. I don't know if it will matter but I always prefer to add fully constructed elements to any container, just in case the container (message, Swing panel, XML element, etc) makes a copy of the content.
 
John Hutcheson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:BodyPart also implements Part.
I think I've cracked it. By wrapping the alternative Multipart in a bodypart and adding the bodypart to the "related" multipart. If this is right thanks for helping think it through.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds right to me.
 
John Hutcheson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:That sounds right to me.

Your last post cam thru just as i was exam the email from my last test run.
result =
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!