1) write a java class with main method as below.
=============================================================
package com.client.utilities;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
/**
* @param args
*/
public static void main(String[] args) throws SendFailedException {
String fromAddress="frommyself@compnay.com";
String toAddress="toyourself@company.com";
String subject="Hello am Sending mail from commandline";
String body="This is a test mail from JavaMailAPI";
try{
//prepare the properties
Properties props = new Properties();
/* if your working in a company , you can ask your network team for the name of SMTP host server of your
comapnny OR instead you can ask IPaddress of the SMTP host serevr */
//Replase the value assigned to smtpHostName String with your smtphost name
//eg : String smtpHostName="121.34.56.78";
String smtpHostName="name of the smtp host server";
props.put("mail.smtp.host", smtpHostName);
Session session = Session.getInstance(props, null);
//Create a MImeMessage MimeMessage message = new MimeMessage(session);
MimeMessage message = new MimeMessage(session);
InternetAddress from = new InternetAddress(fromAddress);
//Set from address to message
message.setFrom(from);
InternetAddress to = new InternetAddress(toAddress);
//Set to address to message , if you want to send mail to multiple reciepents you have to use
//setRecipient method
message.addRecipient(Message.RecipientType.TO, to);
//set the subject
message.setSubject(subject);
//set the body
message.setText(body);
//calling send method of javax.mail.Transport class
Transport.send(message);
}catch (SendFailedException sendfailedException)
{
sendfailedException.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
========================================================
2) using eclipseIDE , you can export the above java class as JAR.please see the stpes
step 1 : right click on the java class , Export-> expnad Java->select JARfile ->next->
step 2: in the JAR file specification page ,keep all the options set to default checks ,don't change any thing
step 3: in the same page give JAR file location where do you want to export lets say for eg: C:\SendMail.jar
step4:clcik next->next->finish
3)step2 will create JAR file at C drive. extract the jar file as like zip file. you can see the folder META-INF and your compiled class file folder(com/client/utilities).
4)open META-INF folder , open the file MANIFEST.MF file.
5) chnage the contents of MANIFEST.MF file as below.
Manifest-Version: 1.0com.client.utilities
Name: com/client/utilities/
Main-Class: com.client.utilities.SendMail
Class-Path: /apache-tomcat-6.0.20/apache-tomcat-6.0.20/lib/mail.jar /apache-tomcat-6.0.20/apache-tomcat-6.0.20/lib/activation.jar
Sealed: true
6)in the above step , to work with sending mail from java application you need to have mail.jar and activation.ajr, if you don't have these 2 files ,you can downlaod this from SUN downloads.
after that you keep this in the tomcat lib folder as a best practice. it is not mandatory to keep in the tomcat lib folder, you can create folder in C drive as C:\JARS , then place 2 jars inside this folder.but you need to change the Class-Path attribute of MANIFEST.MF file as
Class-Path: /JARS/mail.jar /JARS/activation.jar.
save the file.
7)zip the META-INF folder , this will have the MANIFEST.MF file which you saved in the above step. +your class file folder (com/client/utilities/SendMail.class) , rename the zip as SendMail.jar.
8)Now your jar file is ready , C:\SendMail.jar
9)run this jar file from command prompt as below
java -jar SendMail.jar
Please Comment about this post. if you have any questions also you can ask me.