Below command starts James Mail Server as background process.
./run.sh &
./run.sh &
# dos2unix example.sh
#Start java application as a background process nohup java -jar app.jar & #write the process id to file echo $! > PID
#kills the background java process kill -9 `cat PID` #clean up the process ID file rm -rf PID
package com.test;
import java.io.File;
/**
* Shows all the files in a directory and its sub directories
* @author luckyacademy
*
*/
public class ListFiles {
public static void main(String[] args) {
showFiles(new File("C:\\dell"));
}
/**
* Recursively shows all the files.
*/
private static void showFiles(File dirPath) {
for (File file : dirPath.listFiles()) {
if (file.isDirectory()){ //if it is directory, traverse the directory recursively
showFiles(file);
}
System.out.println("file Name: " + file.getAbsolutePath());
}
}
}
nohup java -jar sample.jar &
#configuration properties #key=value username=abc password=123PropertyReader reades the config.properties
package demo;
import java.io.FileInputStream;
import java.util.Properties;
/**
* Reads properties file and display all the contents in it
* @author luckyacademy
*
*/
public class PropertyReader {
public static void main(String[] args) throws Exception {
//open input stream to properties file
FileInputStream fin = new FileInputStream("config.properties");
Properties pr = new Properties();
// read properties file
pr.load(fin);
//read value for key username
System.out.println("Reading value: " + pr.getProperty("username"));
//display all properties in file.
for(Object key:pr.keySet()){
System.out.println("Key: "+key+ " Value: "+pr.getProperty((String)key));
}
//close the properties file
fin.close();
}
}
package demo;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
/**
* Send mail using SMTP Protocol
*
* @author luckyacademy
*/
public class SendMailDemo {
public static void main(String[] args) throws Exception {
String from = "abc@gmail.com";
String to = "contact@localhost";
String subject = "Demo Subject";
String body = "Demo Body";
Properties props = new Properties();
// mail server host name and port
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "19025");
// Get Session
Session mailSession = Session.getDefaultInstance(props);
// Create a message object
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = new InternetAddress(from);
InternetAddress toAddress = new InternetAddress(to);
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(body);
// Send Message
Transport.send(simpleMessage);
}
}