Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

JavaMail not working

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just spent several hours trying everything I can find to send mail from Java and nothing works. I just get "java.lang.NoClassDefFoundError: javax/activation/DataSource". I have the activation.jar and all the JavaMail.jars in my classpath. I tried several code samples from on the web - all different techniques, but ultimately using the JAF and JavaMail - and they all failed with this error when they try to create a MimeMessage object.
Anyone else have this problem?
Tomcat 5
Code (this particular code is straight from oreilly book):
package com.jspservletcookbook;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmailBean {
private final static String DEFAULT_CONTENT = "Unknown content";
private final static String DEFAULT_SUBJECT= "Unknown subject";
private static String DEFAULT_SERVER = null;
private static String DEFAULT_TO = null;
private static String DEFAULT_FROM = null;
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("email.mailDefaults");
DEFAULT_TO = bundle.getString("DEFAULT_TO");
DEFAULT_FROM = bundle.getString("DEFAULT_FROM");
//JavaBean properties
private String smtpHost;
private String to;
private String from;
private String content;
private String subject;
public void sendMessage() throws Exception {
Properties properties = System.getProperties();
//populate the 'Properties' object with the mail
//server address, so that the default 'Session'
//instance can use it.
properties.put("", smtpHost);
Session session = Session.getDefaultInstance(properties);
Message mailMsg = new MimeMessage(session);//a new email message
InternetAddress[] addresses = null;
try {

if (to != null) {
//throws 'AddressException' if the 'to' email address
//violates RFC822 syntax
addresses = InternetAddress.parse(to, false);
mailMsg.setRecipients(Message.RecipientType.TO, addresses);
} else {
throw new MessagingException(
"The mail message requires a 'To' address.");
if (from != null) {
mailMsg.setFrom(new InternetAddress(from));
} else {
throw new MessagingException(
"The mail message requires a valid 'From' address.");
if (subject != null)
if (content != null)
//Finally, send the mail message; throws a 'SendFailedException'
//if any of the message's recipients have an invalid address
} catch (Exception exc) {
throw exc;
private void handleMessages(HttpServletRequest request, PrintWriter out) throws IOException, ServletException {
HttpSession httpSession = request.getSession();
String user = (String) httpSession.getAttribute("user");
String password = (String) httpSession.getAttribute("pass");
String popAddr = (String) httpSession.getAttribute("pop");
Store popStore = null;
Folder folder = null;
if (! check(popAddr))
popAddr = EmailBean.DEFAULT_SERVER;
try {
if ((! check(user)) || (! check(password)))
throw new ServletException("A valid username and password is required to check email.");
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
popStore = session.getStore("pop3");
popStore.connect(popAddr, user, password);
folder = popStore.getFolder("INBOX");
if (! folder.exists())
throw new ServletException("An 'INBOX' folder does not exist for the user.");;
Message[] messages = folder.getMessages();
int msgLen = messages.length;
if (msgLen == 0)
out.println("<h2>The INBOX folder does not yet contain any email messages.</h2>");
for (int i = 0; i < msgLen; i++){
displayMessage(messages[i], out);
out.println("<br /><br />");
} catch (Exception exc) {
out.println("<h2>Sorry, an error occurred while accessing the email messages.</h2>");
} finally {
if (folder != null)
if (popStore != null)
} catch (Exception e) { }
private void displayMessage(Message msg, PrintWriter out) throws MessagingException, IOException {
if (msg != null && msg.getContent() instanceof String){
if (msg.getFrom()[0] instanceof InternetAddress) {
out.println("Message received from: " + ((InternetAddress)msg.getFrom()[0]).getAddress() +"<br />");
out.println("Message received on: " + msg.getReceivedDate() +"<br />");
out.println("Message content type: " + msg.getContentType() +"<br />");
out.println("Message content type: " + (String) msg.getContent());
} else{
out.println("<h2>The received email message was not of a text content type.</h2>");
public void setSmtpHost(String host){
if (check(host)){
this.smtpHost = host;
} else {
this.smtpHost = EmailBean.DEFAULT_SERVER;
public void setTo(String to){
if (check(to)){ = to;
} else { = EmailBean.DEFAULT_TO;
public void setFrom(String from){
if (check(from)){
this.from = from;
} else {
this.from = EmailBean.DEFAULT_FROM;
public void setContent(String content){
if (check(content)){
this.content = content;
} else {
this.content = EmailBean.DEFAULT_CONTENT;
public void setSubject(String subject){
if (check(subject)){
this.subject = subject;
} else {
this.subject = EmailBean.DEFAULT_SUBJECT;
private boolean check(String value){
if(value == null || value.equals(""))
return false;
return true;
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to "Other Java APIs", which is where JavaMail questions belong.
Dimitri Dimo
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello? Anyone here?
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dimitri,
I am getting the same error. Did you ever find a resolution? I just upgraded from Tomcat 4.1.18 to Tomcat 5, and am now getting the error.
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott and Dimitri
Can you let us know where in classpath you have put those jars?
In Tomcat you can try putting there in the lib folder where other Jars are already there. As I remember last from Tomcat experience you can also modify setclasspath.batch or to include the jar file wherever youput.
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
    Bookmark Topic Watch Topic
  • New Topic