Link to home
Start Free TrialLog in
Avatar of jrod
jrod

asked on

JavaMail - Need help creating multipart message/email!

I am trying to set a mime message with html and text as a multipart "alternative" message and add an attachment. I have the html stored in a string and the text stored in a string. I do not yet have the attachment, but I am assuming that I would just read this into a string also?!? Here is my code so far. I can send email with text or html, but not with both. What am I missing and where would I place the attachment? What import statements do I need to have (show current imports)?

import javax.swing.JOptionPane;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
...
MimeBodyPart textpart=new MimeBodyPart();
textpart.setText(text);
textpart.addHeaderLine("Content-Type: text/plain; charset=\"iso-8859-1\"");
textpart.addHeaderLine("Content-Transfer-Encoding: quoted-printable");

MimeBodyPart htmlpart=new MimeBodyPart();
htmlpart.setText(html,"text/html");
htmlpart.addHeaderLine("Content-Type: text/html; charset=\"iso-8859-1\"");
htmlpart.addHeaderLine("Content-Transfer-Encoding: quoted-printable");

Multipart mp=new MimeMultipart("related");
Multipart mp2=new MimeMultipart("alternative");

mp2.addBodyPart(textpart);
mp2.addBodyPart(htmlpart);

MimeBodyPart bp=new MimeBodyPart();
bp.setContent(mp);

mp.addBodyPart(bp);
message.setContent(mp);

...
ASKER CERTIFIED SOLUTION
Avatar of Venci75
Venci75

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Venci75
Venci75

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Venci75
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange