?
Solved

JavaMail - Need help creating multipart message/email!

Posted on 2002-04-15
3
Medium Priority
?
487 Views
Last Modified: 2013-11-24
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);

...
0
Comment
Question by:jrod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 6944480
try this:


Multipart mp2=new MimeMultipart("Related");
MimeBodyPart textpart=new MimeBodyPart();
MimeBodyPart htmlpart=new MimeBodyPart();

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

textpart.setText("TEXT");
htmlpart.setContent("<a href=\"http://www.yahoo.com\">click here</a>", "text/html");

message.setContent(mp2);
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7940726
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
0
 

Expert Comment

by:modulo
ID: 7990894
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
A solution for Fortify Path Manipulation.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question