Solved

JavaMail - Need help creating multipart message/email!

Posted on 2002-04-15
3
483 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 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 38
jboss 7.1 start up error 1 63
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 43
import as existing maven project 3 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

749 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