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);
}
}
package demo;
import java.net.URI;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.FindItemsResults;
import microsoft.exchange.webservices.data.Item;
import microsoft.exchange.webservices.data.ItemView;
import microsoft.exchange.webservices.data.WebCredentials;
import microsoft.exchange.webservices.data.WellKnownFolderName;
public class EWSDemo {
public static void main(String[] args) throws Exception {
ExchangeService service = new ExchangeService();
// Provide Crendentials
ExchangeCredentials credentials = new WebCredentials("username", "password");
service.setCredentials(credentials);
// Set Exchange WebSevice URL
service.setUrl(new URI("https://myexchangehost/EWS/exchange.asmx"));
// Get five items from mail box
ItemView view = new ItemView(5);
// Search Inbox
FindItemsResults- findResults = service.findItems(WellKnownFolderName.Inbox, view);
// iterate thru items
for (Item item : findResults.getItems()) {
System.out.println(item.getSubject());
}
}
}
Reading Body item.getBody()Reading body using the method item.getBody() it yields to exception
Exception in thread "main" microsoft.exchange.webservices.data.ServiceObjectPropertyException: You must load or assign this property before you can read its value.To overcome it first the body needs to be loaded.
item.load();Reading Non default mail box
Mailbox mb = new Mailbox();
mb.setAddress("syadmin@abc.com");
FolderId folderId = new FolderId(WellKnownFolderName.Inbox, mb);
// Search Inbox syadmin@abc.com
FindItemsResults- findResults = service.findItems( folderId, view);
Reading UnRead Emails
// Get five items from mail box ItemView view = new ItemView(5); //Set Filter to Read UnRead emails SearchFilter itemFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true); // Search Inbox FindItemsResultsMarking mail as read- findResults = service.findItems(WellKnownFolderName.Inbox,itemFilter,view); // iterate thru items for (Item item : findResults.getItems()) { System.out.println(item.getSubject()); }
// iterate thru items
for (Item item : findResults.getItems()) {
//Type cast it to EmailMessage
EmailMessage em = (EmailMessage) item;
em.setIsRead(true);
em.update(ConflictResolutionMode.AlwaysOverwrite);
System.out.println(em.getSubject());
}
Reading mail Headers and body
// iterate thru items
for (Item item : findResults.getItems()) {
item.load();
String subject=item.getSubject();
//Read Body
String body = MessageBody.getStringFromMessageBody(item.getBody());
System.out.println(subject);
System.out.println(body);
//Reading Header
InternetMessageHeaderCollection headers = item.getInternetMessageHeaders();
for(InternetMessageHeader header:headers){
System.out.println("Header Name: "+header.getName()+" Header Value: "+header.getValue() );
}
}