Solved

JavaMail - Need help creating multipart message/email!

Posted on 2002-04-15
3
479 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
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 50 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 50
create a gui in perl 3 46
backtracking recursion  code 19 41
micro services spring boot application error 3 43
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now