MailSender.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
public class MailSender {
public static Logger logger = Logger.getLogger(MailSender.class);
public static boolean send(Mail mail) throws Exception {
try {
Properties props = new Properties();
props.put("mail.smtp.host", "localhost");
Session session = Session.getDefaultInstance(props, null);
MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setFrom(new InternetAddress(mail.getFrom()));
mimemessage.setSentDate(mail.getDate());
// set SUBJECT
mimemessage.setSubject(mail.getSubject());
// set TO address
String mailto = mail.getTo();
String ccmailid = mail.getCcusers();
String strResult = "";
try {
mimemessage.setRecipients(javax.mail.Message.RecipientType.TO,
mailto);
} catch (Exception exception1) {
throw exception1;
}
// set message BODY
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setText(mail.getContent());
// attach message BODY
MimeMultipart mimemultipart = new MimeMultipart();
mimemultipart.addBodyPart(mimebodypart);
// attach FILE
ArrayList attachedFileList = mail.getAttachedFileList();
if (attachedFileList != null) {
DataSource ds = null;;
for (Iterator e = attachedFileList.iterator(); e.hasNext();) {
ds = (DataSource) e.next();
mimebodypart = new MimeBodyPart();
try {
mimebodypart.setDataHandler(new DataHandler(
ds));
} catch (Exception exception3) {
throw exception3;
}
mimebodypart.setFileName(ds.getName()); // set FILENAME
mimemultipart.addBodyPart(mimebodypart);
}
}// end if
mimemessage.setContent(mimemultipart);
// set CC MAIL and SEND the mail
if (!mailto.equals("")) {
// set CC MAIL
if (ccmailid != null && (!ccmailid.equals("")))
mimemessage.setRecipients(
javax.mail.Message.RecipientType.CC, ccmailid);
try {
// send MAIL
Transport.send(mimemessage);
logger.info(mailto + " Sent Successfully..........");
} catch (Exception exception4) {
throw exception4;
}
} else {
logger.info(mailto + " Mail operation Failed..........");
}
} catch (Exception e) {
throw e;
}
return true;
}
}
Mail.java
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;
public class Mail {
private String from = null;
private String to = null;
private String subject = null;
private String content = null;
private String ccusers = null;
private ArrayList attachedFileList = null;
private Date date = null;
public Mail() {
// TODO Auto-generated constructor stub
}
public ArrayList getAttachedFileList() {
return attachedFileList;
}
public void setAttachedFileList(ArrayList attachedFileList) {
this.attachedFileList = attachedFileList;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getCcusers() {
return ccusers;
}
public void setCcusers(String ccusers) {
this.ccusers = ccusers;
}
}