I am writing a simple class to send email, so there are external JARs required. What I did was I added the External JARS in eclipse project. It is working.
But when I call the class from command line it gives NoClassDefFoundError:javax.mail.Address. Two things here, I had copied all the JARs in Java Lib folder and second is that javax.mail.Address is not directly referenced in code. Any idea what is woring or how to fix it.
The class has a main function which is working fine without the email code.
p.s
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailNotifications {
protected static String message_loseEvent ;
protected static Properties properties;
protected static Connection connection;
protected static Session session;
public static void main(String [] args)throws Exception
{
loadPropertiesFromXML("applicatiosnSettings.xml");
setupMailServer();
_openDBConnection();
ResultSet R = _executeQuery( getQuery("TodaysLossEventQuery") );
while(R.next()) {
message_loseEvent = "<table border=1 ><tr><td>Name</td><td>Description</td><td>URL</td></tr><tr><td>" + R.getString( R.findColumn("Name") ) + "</td><td>" + R.getString( R.findColumn("Description") ) + "</td><td><a href='" + R.getString( R.findColumn("DetailURL") ) + "'>See on OpenPages</a></td></tr></table>";
}
Email("openpages@fvb.com","subject","content");
connection.close();
}
protected static Properties loadPropertiesFromXML(String filePath)
{
try
{
File file = new File(filePath);
FileInputStream fileInput = new FileInputStream(file);
properties = new Properties();
properties.loadFromXML(fileInput);
fileInput.close();
return properties;
}catch (Exception e)
{
e.printStackTrace();
}
return null;
}
protected static String _getProperty(String key)
{
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
String _key = (String) enuKeys.nextElement();
if(_key.equals(key))
{
// System.out.println( " _getProperty() > " + properties.getProperty(key));
return properties.getProperty(key);
}
}
return null;
}
protected static void _openDBConnection()
throws ClassNotFoundException, SQLException, IOException
{
//working Drivers
Class.forName ( "COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver" );
//Class. forName ( "com.ibm.db2.jcc.DB2Driver" );
String db_host = _getProperty("db_host");
String db_port = _getProperty("db_port");
String db_name = _getProperty("db_name");
String db_user = _getProperty("db_username");
String db_password = _getProperty("db_password");
connection = DriverManager.getConnection("jdbc:db2://"+db_host+":"+db_port+"/"+db_name,db_user,db_password);
//System.out.println( "> DataBase connection obtained " );
}
protected static String getQuery(String queryTitle)
{
return _getProperty(queryTitle);
}
//SELECT OPAGES.RT_LossEvent.LOSSEVENT_ID as LossEventId,OPAGES.RT_LossEvent.NAME00 as Name,OPAGES.RT_LossEvent.DESCRIPTION as Description,OPAGES.ACTORINFO.EMAIL as EmailAddress, OPAGES.RT_LossEvent.DETAIL_PAGE_URL as DetailURL FROM OPAGES.RT_LossEvent JOIN OPAGES.ACTORINFO ON (OPAGES.ACTORINFO.NAME = OPAGES.RT_LossEvent.OWNER AND CONCAT(CONCAT(Year(current date),'-'),CONCAT(Month(current date),CONCAT('-',Day(current date)))) = CONCAT(CONCAT(Year(OPAGES.RT_LossEvent.Creation_Date),'-'),CONCAT(Month(OPAGES.RT_LossEvent.Creation_Date),CONCAT('-',Day(OPAGES.RT_LossEvent.Creation_Date)))))
//ResultSet R
protected static ResultSet _executeQuery(String _query)
throws ClassNotFoundException, SQLException, IOException {
//System.out.println(_query);
Statement s = connection.createStatement();
return s.executeQuery(_query);
//while(R.next()) {
// message_loseEvent = "<table border=1 ><tr><td>Name</td><td>Description</td><td>URL</td></tr><tr><td>" + R.getString( R.findColumn("Name") ) + "</td><td>" + R.getString( R.findColumn("Description") ) + "</td><td><a href='" + R.getString( R.findColumn("DetailURL") ) + "'>See on OpenPages</a></td></tr></table>";
//}
}
protected static void setupMailServer()
{
// Recipient's email ID needs to be mentioned.
String to = _getProperty("from_address");
// Sender's email ID needs to be mentioned
String from = _getProperty("from_address");
// Assuming you are sending email from localhost
String host = _getProperty("email_host");
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty( _getProperty("email_server") , host);
// Get the default Session object.
session = Session.getDefaultInstance(properties);
}
protected static void Email(String _toAddress,String _subject,String _content)
{
try
{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress( _getProperty("from_address") ));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(_toAddress));
// Set Subject: header field
message.setSubject(_subject);
// Send the actual HTML message, as big as you like
message.setContent(message_loseEvent,"text/html" );
// Send message
Transport.send(message);
//System.out.println("> Sent message successfully");
}catch (MessagingException mex)
{
mex.printStackTrace();
}
}
}