Solved

Java Mail

Posted on 2010-09-08
7
387 Views
Last Modified: 2012-05-10
Hi Expert

I have a sample code that sends out mail but I got this Error


Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource
      at MailClient.sendMail(MailClient.java:21)
      at MailClient.main(MailClient.java:55)

it's  failing at not sure why

      Message message = new MimeMessage(session);



import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activity.*;
import javax.sql.DataSource;

public class MailClient {

	public void sendMail(String mailServer, String from, String to,
			String subject, String messageBody) throws MessagingException,
			AddressException {
		// Setup mail server
		Properties props = System.getProperties();
		props.put("mail.smtp.host", mailServer);

		// Get a mail session
		Session session = Session.getDefaultInstance(props, null);

		// Define a new mail message
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		message.setSubject(subject);

		// Create a message part to represent the body text
		BodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setText(messageBody);

		// use a MimeMultipart as we need to handle the file attachments
		Multipart multipart = new MimeMultipart();

		// add the message body to the mime message
		multipart.addBodyPart(messageBodyPart);

		// add any file attachments to the message

		// Put all message parts in the message
		message.setContent(multipart);

		// Send the message
		Transport.send(message);

	}

	public static void main(String[] args) {
		try {
			MailClient client = new MailClient();
			String server = "mailserver.com";
			String from = "Anthonio.Booysen@investec.co.za";
			String to = "Anthonio.Booysen@investec.co.za";
			String subject = "Test";
			String message = "Testing";

			client.sendMail(server, from, to, subject, message);
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}

	}
}

Open in new window

0
Comment
Question by:abooysen2
[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
  • 3
  • 3
7 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 125 total points
ID: 33634115
you should have activation.jar in the classpath!
0
 

Author Comment

by:abooysen2
ID: 33634126
add the activation.jar got a new Error now. Is this on my side or just a fire wall issue

javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
      class com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for Anthonio.Booysen@investec.co.za

      at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1130)
      at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:525)
      at javax.mail.Transport.send0(Transport.java:151)
      at javax.mail.Transport.send(Transport.java:80)
      at MailClient.sendMail(MailClient.java:42)
      at MailClient.main(MailClient.java:55)
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33634135
it may be an invalid address as per the SMPT server you use, talk to your adminsitrator!
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33634146
0
 

Author Comment

by:abooysen2
ID: 33634151
I took the smtp server address from a other project and verified it with my outlook. Does this mean the firewall does not allow me to sent mails?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33634257
could be an authentication issue... check with mail server admin...
0
 

Author Comment

by:abooysen2
ID: 33634266
ok Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

735 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