package javajunction;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaJunctionMailServer extends javax.mail.Authenticator
{
private String mailhost ="smtp.gmail.com"; //"smtp.mail.yahoo.com"; //"smtp.gmail.com";
private String user;
private String password;
private Session session;
{
private String mailhost ="smtp.gmail.com"; //"smtp.mail.yahoo.com"; //"smtp.gmail.com";
private String user;
private String password;
private Session session;
public JavaJunctionMailServer(String user, String password) {
this.user = user;
this.password = password;
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, password);
}
{
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
throw new IOException("Not Supported");
}
}
}
JavaJunctionDBScheduler.java
package java4s;
import java.util.Timer;
import javaConstants.Constants;
import java.util.Timer;
import javaConstants.Constants;
public class JavaJunctionDBScheduler
{
{
public void callScheduler() throws Exception
{
{
System.out.println("Scheduler Starterd...");
ReadPropertiesFile.readConfig();
Timer timer = new Timer();
ReadPropertiesFile.readConfig();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new Testing(), getTimePrecision(Constants.delay), getTimePrecision(Constants.timetoquery));
}
public long getTimePrecision(String value) throws Exception
{
long l = 0;
String val="";
try
{
if(value.endsWith("d") || value.endsWith("D"))
{
val = value.substring(0,value.length()-1);
l = Long.parseLong(val) * 24 * 60 * 60 * 1000;
}
{
long l = 0;
String val="";
try
{
if(value.endsWith("d") || value.endsWith("D"))
{
val = value.substring(0,value.length()-1);
l = Long.parseLong(val) * 24 * 60 * 60 * 1000;
}
else if(value.endsWith("h") || value.endsWith("H"))
{
{
val = value.substring(0,value.length()-1);
l = Long.parseLong(val) * 60 * 60 * 1000;
l = Long.parseLong(val) * 60 * 60 * 1000;
}
else if(value.endsWith("m") || value.endsWith("M"))
{
val = value.substring(0,value.length()-1);
l = Long.parseLong(val) * 60 * 1000;
}
else if(value.endsWith("s") || value.endsWith("S"))
{
else if(value.endsWith("m") || value.endsWith("M"))
{
val = value.substring(0,value.length()-1);
l = Long.parseLong(val) * 60 * 1000;
}
else if(value.endsWith("s") || value.endsWith("S"))
{
val = value.substring(0,value.length()-1);
l = Long.parseLong(val) * 1000;
}
else
{
l = Long.parseLong(val) * 1000;
}
else
{
l = Long.parseLong(value);
}
}
}
catch(Exception e)
{
catch(Exception e)
{
throw new Exception(e);
}
}
return l;
}
public static void main(String a[]) throws Exception
{
DBScheduler dbs = new DBScheduler();
dbs.callScheduler();
}
}
public static void main(String a[]) throws Exception
{
DBScheduler dbs = new DBScheduler();
dbs.callScheduler();
}
}
ReadPropertiesFile.java
package javajunction;
import java.io.FileInputStream;
import java.util.Properties;
import javajunctionConstants.Constants;
import java.util.Properties;
import javajunctionConstants.Constants;
public class ReadPropertiesFile
{
public static void readConfig() throws Exception
{
try
{
{
public static void readConfig() throws Exception
{
try
{
Properties pro = new Properties();
String path = System.getProperty("user.dir")+"/javajunction_Props.properties";
pro.load(new FileInputStream(path));
String path = System.getProperty("user.dir")+"/javajunction_Props.properties";
pro.load(new FileInputStream(path));
Constants.delay = pro.getProperty("delay");
Constants.timetoquery = pro.getProperty("timetoquery");
Constants.setFrom = pro.getProperty("setFrom");
Constants.setPassword = pro.getProperty("setPassword");
Constants.emailTO = pro.getProperty("emailTO");
Constants.timetoquery = pro.getProperty("timetoquery");
Constants.setFrom = pro.getProperty("setFrom");
Constants.setPassword = pro.getProperty("setPassword");
Constants.emailTO = pro.getProperty("emailTO");
}
catch(Exception e)
{
throw new Exception(e);
}
catch(Exception e)
{
throw new Exception(e);
}
}
}
JavaJunctionTesting.java
package javajunction;
import java.util.TimerTask;
import javaConstants.Constants;
import javaConstants.Constants;
public class JavaJunctionTesting extends TimerTask
{
{
public void run()
{
{
JavaJunctionMailServer sender = new JavaJunctionMailServer(Constants.setFrom, Constants.setPassword);
try {
sender.sendMail("Subject","This is JavaJunction",Constants.setFrom,Constants.emailTO);
}
catch (Exception e) {
e.printStackTrace();
}
sender.sendMail("Subject","This is JavaJunction",Constants.setFrom,Constants.emailTO);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Email Sent Succesfully...");
}
}
}
Javajunction_Props.properties
setFrom = your from email id
setPassword = your password
setPassword = your password
#repeat for every 10 seconds
timetoquery = 10s
timetoquery = 10s
#start after 2seconds for first time..
delay = 2s
delay = 2s
emailTO = your to email id
JavaJunctionConstants.constants
package javajunction;
package javajunction;
public class JavaJunctionConstants
{
public static String delay;
public static String timetoquery;
public static String setFrom;
public static String setPassword;
public static String emailTO;
}
{
public static String delay;
public static String timetoquery;
public static String setFrom;
public static String setPassword;
public static String emailTO;
}
No comments:
Post a Comment