Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JavaMail - Need help creating multipart message/email!

Posted on 2002-04-15
3
Medium Priority
?
490 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

610 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