How to write the batch for sample java class and scheduling task in task scheduler
Introduction:
Basically, windows allow .bat file Linux allows .sh file here I am writing the batch file for sample java program.
Here my java class fetch my system IP number and send an email to the specified user which I am provided in java class.
Here is my java class
Explanation:
Explanation:
Basically, windows allow .bat file Linux allows .sh file here I am writing the batch file for sample java program.
Here my java class fetch my system IP number and send an email to the specified user which I am provided in java class.
Here is my java class
import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * * @author Ram Janardhan Randhi */ public class mail { public static void main(String[] args) throws UnknownHostException { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "587"); String from = "****************"; //email id String password = "***************"; //password String to = "**********"; //Destination(Receiver) email String sub = "Updated IP_Address"; InetAddress localhost = InetAddress.getLocalHost(); String msg = localhost.getHostAddress().trim(); //get Session Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); //compose message try { MimeMessage message = new MimeMessage(session); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(sub); message.setText(msg); //send message Transport.send(message); System.out.println("message sent successfully"); } catch (MessagingException e) { throw new RuntimeException(e); } } }
- Here I am providing the Gmail SMTP details for sending emails to users.
- First, create the properties class instance. it used to load the properties of SMTP.
- Using the javax.mail.Authenticator() method used for weather user given details authenticated or not.
- Using the session object(authentication response) to be the mime message instance.
- Using predefined methods like addRecipient,setSubject,setText, send methods to send an email to the destination user.
- Here i am using the InetAddress localhost = InetAddress.getLocalHost(); String msg = localhost.getHostAddress().trim(); these are used for getting the ip address of my system.
push D:\projects\DailySchedular set PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_92\bin SET CLASSPATH=D:\projects\DailySchedular\activation.jar;D:\projects\DailySchedular\java-mail-1.4.4.jar;. javac mail.java java mail
Explanation:
- push command is used for command line editor change the directory to your project location.
- set path used for setting the java path. (java executable file location).
- set classpath used to add the libraries which are used to compile the java program.
- javac filename.java used to compile the java program.
- java filename used for executing the class file.
0 Response to "How to write the batch for sample java class and scheduling task in task scheduler"
Post a Comment