12/22/09

Java mail: pick data from csv and db for mailing using oops

We shall make use of an interface,for re-usability. mailinterface in the master package. it has one interface and a class.
csvdata is another package . reads from csv file
jdbcdata is another pacakage reads from database


sendmail is another pacakage. here you can specify whether you want csv or db.

package mailinterface;

/**
*
 * @author raghuram
*/
public interface MailImplData {


// this method shall be used to pick different from,to addresses and sub and content
public void readDifferentFromAddress();
// this method shall be used to pick different to addresses and sub and content but same from address
public void readSameFromAddress();
// this method shall be used to pick same from addresses and sub and content but different to address
public void readRecipientOnly();
}


package mailinterface;

/**
*
 * @author raghuram
*/
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMailTest {
public void send(String smtpHost, int smtpPort, String from, String to, String subject,
String content) throws AddressException, MessagingException {

java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "" + smtpPort);
Session session = Session.getDefaultInstance(props, null);

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(content);

Transport.send(msg);
}
}



package csvdata;

import java.util.StringTokenizer;
import java.io.*;
import java.util.*;
import mailinterface.*;

/**
*
 * @author raghuram
*/
public class ReadCSV implements MailImplData {

Properties props;

public ReadCSV() {
try {
props = new Properties();
props.load(new FileInputStream("ForMail.properties"));

} catch (IOException e) {
}
}
// this method shall be used to pick different from,to addresses and sub and content
public void readDifferentFromAddress() {
try {
//String fName = "test.csv";
String thisLine;
SendMailTest smt = new SendMailTest();
int count = 0;

FileInputStream fis = new FileInputStream(props.getProperty("csv.url.different"));

DataInputStream myInput = new DataInputStream(fis);

int y = 0;
int i = 0;
while ((thisLine = myInput.readLine()) != null) {
StringTokenizer st = new StringTokenizer(thisLine, ",");
String strar[] = thisLine.split(",");
while (st.hasMoreElements()) {
//a[i]=st.nextToken();
//b[i]=st.nextToken();
smt.send(props.getProperty("smtp.host"), Integer.parseInt(props.getProperty("smtp.port")), st.nextToken(), st.nextToken(), st.nextToken(), st.nextToken());

i++;


System.out.println();
}
}






package jdbcdata;

import java.sql.*;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import mailinterface.*;
/**
*
 * @author raghuram
*/
public class ReadDB implements MailImplData{

Properties props;

public ReadDB() {
try {
props = new Properties();
props.load(new FileInputStream("ForMail.properties"));

} catch (Exception e) {
}
}
// this method allows different from_add, to_add,sub,content
public void readDifferentFromAddress() {
ConnectionManager conMg = new ConnectionManager();
Connection con = null;
Statement stat = null;
ResultSet rs = null;
try {
con = conMg.createConnection();
stat = con.createStatement();
SendMailTest smt = new SendMailTest();
rs = stat.executeQuery("Select * from mail_details");
int i = 0;
while (rs.next()) {
smt.send(props.getProperty("smtp.host"), Integer.parseInt(props.getProperty("smtp.port")), rs.getString("FROM_ADDRESS"), rs.getString("TO_ADDRESS"), rs.getString("SUBJECT"), rs.getString("CONTENT"));
// names[i] = rs.getString("TABLE_NAME");
i++;

}


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stat.close();
} catch (SQLException ex) {
Logger.getLogger(ReadDB.class.getName()).log(Level.SEVERE, null, ex);
}
conMg.close();
}
}
// this method allows different to_add,sub,content but same from_add
public void readSameFromAddress() {
ConnectionManager conMg = new ConnectionManager();
Connection con = null;
Statement stat = null;
ResultSet rs = null;
try {
con = conMg.createConnection();
stat = con.createStatement();
SendMailTest smt = new SendMailTest();
rs = stat.executeQuery("Select * from mail_details");
int i = 0;
while (rs.next()) {

smt.send(props.getProperty("smtp.host"), Integer.parseInt(props.getProperty("smtp.port")), props.getProperty("from.address"), rs.getString("TO_ADDRESS"), rs.getString("SUBJECT"), rs.getString("CONTENT"));

i++;

}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stat.close();
} catch (SQLException ex) {
Logger.getLogger(ReadDB.class.getName()).log(Level.SEVERE, null, ex);
}
conMg.close();
}
}
// this method allows same from_add,sub,content but different recipient
public void readRecipientOnly() {
ConnectionManager conMg = new ConnectionManager();
Connection con = null;
Statement stat = null;
ResultSet rs = null;
try {
con = conMg.createConnection();
stat = con.createStatement();
SendMailTest smt = new SendMailTest();
rs = stat.executeQuery("Select * from recipient");
int i = 0;
while (rs.next()) {

smt.send(props.getProperty("smtp.host"), Integer.parseInt(props.getProperty("smtp.port")), props.getProperty("from.address"), rs.getString("TO_ADDRESS"), props.getProperty("subject.common"), props.getProperty("content.common"));

i++;

}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stat.close();
} catch (SQLException ex) {
Logger.getLogger(ReadDB.class.getName()).log(Level.SEVERE, null, ex);
}
conMg.close();
}
}
}





package sendmail;

//import csvdata.ReadCSV;
import mailinterface.MailImplData;
import java.io.*;
import java.util.*;

/**
*
 * @author raghuram
*/
public class Main {

Properties props;
String data = null;

public void loadProperty() {
try {
props = new Properties();
props.load(new FileInputStream("ForMail.properties"));

} catch (IOException e) {

e.printStackTrace();
}
}

public String getData() {

try {

data = props.getProperty("class.db");
//data = props.getProperty("class.csv");

} catch (Exception e) {
e.printStackTrace();
}
return data;
}

public static void main(String[] args) {
try {
Main mainObj = new Main();
mainObj.loadProperty();
MailImplData impl = (MailImplData) Class.forName(mainObj.getData()).newInstance();
impl.readDifferentFromAddress();
impl.readRecipientOnly();
impl.readSameFromAddress();
} catch (ClassNotFoundException ce) {

ce.printStackTrace();
} catch (InstantiationException ie) {

ie.printStackTrace();
} catch (IllegalAccessException ile) {

ile.printStackTrace();
}
}
---------------------------
sample csv...

sraghuraman@corenttech.com,krypton.raghu@gmail.com,testmail,how are you
skirubakaran@corentech.com,kiruba.matrix@gmail.com,testmail,how are you


No comments:

Post a Comment

Popular Posts