Friday, May 8, 2009

MailSend Java

mailconfig.properties
sentprotocol=smtp
senthost=pop.gmail.com
sentport=995
senderid=your email id
senderpassword=your password
receiveprotocol=pop
receivehost=smtp.gmail.com
receiveport=587
receiverid=receiver mail id
receiverpassword=receiver password
folder=inbox
savedir=/root/tmp
debug=true

MailConfig.java
package mail;

import java.util.ResourceBundle;
public class MailConfig
{
public static ResourceBundle resourceBundle;
public static String SEND_PROTOCOL;
public static String SEND_HOST;
public static String SEND_PORT;
public static String SENDER_ID;
public static String SENDER_PASSWORD;
public static String RECEIVE_PROTOCOL;
public static String RECEIVE_HOST;
public static String RECEIVE_PORT;
public static String RECEIVER_ID;
public static String RECEIVER_PASSWORD;
public static String FOLDER;
public static String SAVE_DIR;
public static String DEBUG;

static{
try{
resourceBundle=ResourceBundle.getBundle("mail.mailconfig");
init();
}catch(Exception ex){
ex.getMessage();
}
init();
}

/**
* this method initialize the mail configuration variables
*
*/
public static void init()
{
SEND_PROTOCOL=resourceBundle.getString("sentprotocol");
SEND_HOST=resourceBundle.getString("senthost");
SEND_PORT=resourceBundle.getString("sentport");
SENDER_ID=resourceBundle.getString("senderid");
SENDER_PASSWORD=resourceBundle.getString("senderpassword");
RECEIVE_PROTOCOL=resourceBundle.getString("receiveprotocol");
RECEIVE_HOST=resourceBundle.getString("receivehost");
RECEIVE_PORT=resourceBundle.getString("receiveport");
RECEIVER_ID=resourceBundle.getString("receiverid");
RECEIVER_PASSWORD=resourceBundle.getString("receiverpassword");
FOLDER=resourceBundle.getString("folder");
SAVE_DIR=resourceBundle.getString("savedir");
DEBUG=resourceBundle.getString("debug");
}
}

MailSender.java
package mail;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.Date;
public class MailSender
{
private String strProtocol;
private String strHost;
private int nPort;
private String strFrom;
private String strPassword;
private Properties prop;
private boolean debug;
private Transport tr = null;
private Session session;


public MailSender()
{
strProtocol=MailConfig.SEND_PROTOCOL;
strHost=MailConfig.SEND_HOST;
nPort=Integer.parseInt(MailConfig.SEND_PORT);
strFrom=MailConfig.SENDER_ID;
strPassword=MailConfig.SENDER_PASSWORD;
debug=Boolean.parseBoolean(MailConfig.DEBUG);
init();
}

public void init()
{
prop=System.getProperties();
prop.put("mail.smtp.host", strHost);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtps.auth", "true");
session=Session.getInstance(prop,null);
if(debug){
session.setDebug(debug);
}
}

/**
* This method send mail to specified mail address from the
* mail address specified in the property file.
* @param subject subject of the mail to be send
* @param to to address of the mail
* @param message the content of the mail
* @return true if the mail sent successfully otherwise false.
*/
public boolean sendMessage(String subject,String to,String message){
boolean status=sendMessage(subject,to,"",message);
return status;
}

/**
* This Method send message to specified mail address form the
* mail address specified specified in the property file.
* @param subject of the mail to be send
* @param to to address of the mail.
* @param cc copy to
* @param message message content
* @return true if the message sent successfully.
*/
public boolean sendMessage(String subject,String to,String cc,String message)
{
try {
Message msg=new MimeMessage(session);
Address[] toAddress=InternetAddress.parse(to,false);
Address[] ccAddress=InternetAddress.parse(cc,false);

if(strFrom!=null){
msg.setFrom(new InternetAddress(strFrom));
}
msg.setRecipients(Message.RecipientType.TO,toAddress);
if(!cc.equals(""))
msg.setRecipients(Message.RecipientType.CC,ccAddress);
msg.setSubject(subject);
msg.setText(message);
msg.setHeader("X-Mailer","msgsend");
msg.setSentDate(new Date());
tr = session.getTransport(strProtocol);
tr.connect(strHost,nPort,strFrom, strPassword);
tr.sendMessage(msg, msg.getAllRecipients());
return true;
}
catch(Exception ex) {
ex.printStackTrace();
}
return false;
}

public static void main(String arsg[])
{
MailSender mailsender=new MailSender();
boolean st=mailsender.sendMessage("test","nimishth@gmail.com","test mail");
System.out.print(st);
}
}

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails