package com.cv.mail;
import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
/**
* @author Chandra Vardhan
*
*/
public class EmailUtils {
private static final Logger LOGGER = Logger.getLogger(EmailUtils.class);
public static void sendEmail(final Properties emailProperties, String content, final File attachment) {
LOGGER.debug("In EmailUtils.sendEmail(- -) method");
try {
if (emailProperties != null && !emailProperties.isEmpty()) {
// Get the session object
final String userName = (String) emailProperties.get("username");
final String password = (String) emailProperties.get("password");
String log = "Mail with attachment sent successfully...";
Session session = Session.getInstance(emailProperties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
if (content != null) {
content = "<p><span face=\"Calibri\" style=\"font-family: Calibri;\"><font size=\"4\">" + content + "</font></p>";
}
Message message = new MimeMessage(session);
if (attachment != null) {
log = "Mail with attachment sent successfully...";
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Create a multipar message
messageBodyPart.setContent(content, "text/html");
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachment.getName());
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
} else {
message.setContent(content, "text/html");
}
log = "Mail sent successfully...";
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse((String) emailProperties.get("to.address")));
message.setRecipients(Message.RecipientType.CC,
InternetAddress.parse((String) emailProperties.get("cc.address")));
message.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse((String) emailProperties.get("bcc.address")));
message.setFrom(new InternetAddress((String) emailProperties.get("from.address")));
message.setSubject((String) emailProperties.get("mail.subject"));
// Now set the actual message
Transport.send(message);
LOGGER.debug(log);
} else {
LOGGER.error("Please provide email properties. There are no email properties.");
}
} catch (
AddressException e) {
LOGGER.error("Problem while sending mail...");
e.printStackTrace();
} catch (MessagingException e) {
LOGGER.error("Problem while sending mail...");
e.printStackTrace();
}
LOGGER.debug("Out EmailUtils.sendEmail(- -) method");
}
} |
No comments:
Post a Comment