jar files required to execute this file : mail.jar,activation.jar,log4j .jar
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
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 EmailClient {
private static Logger logger = Logger.getLogger(EmailClient. class);
public static boolean sendEmail(String fromAddress, String[] toAddress,
String[] ccAddress, String[] bccAddress, String subject,
String message, String htmlMessage, String outgoingHost) {
MimeMultipart mimemultipart = null;
InternetAddress toaddress[] = new InternetAddress[toAddress. length];
InternetAddress ccaddress[] = null;
if (ccAddress != null && ccAddress.length > 0) {
ccaddress = new InternetAddress[ccAddress. length];
}
InternetAddress bccaddress[] = null;
if (bccAddress != null && bccAddress.length > 0) {
bccaddress = new InternetAddress[bccAddress. length];
}
java.util.Properties properties = System.getProperties();
import javax.mail.internet.
import javax.mail.internet.
import javax.mail.internet.
import javax.mail.internet.
import org.apache.log4j.Logger;
public class EmailClient {
private static Logger logger = Logger.getLogger(EmailClient.
public static boolean sendEmail(String fromAddress, String[] toAddress,
String[] ccAddress, String[] bccAddress, String subject,
String message, String htmlMessage, String outgoingHost) {
MimeMultipart mimemultipart = null;
InternetAddress toaddress[] = new InternetAddress[toAddress.
InternetAddress ccaddress[] = null;
if (ccAddress != null && ccAddress.length > 0) {
ccaddress = new InternetAddress[ccAddress.
}
InternetAddress bccaddress[] = null;
if (bccAddress != null && bccAddress.length > 0) {
bccaddress = new InternetAddress[bccAddress.
}
java.util.Properties properties = System.getProperties();
// outgoingHost-is ur smtp host ip
properties.put("mail.smtp. host", outgoingHost);
Session session = Session.getDefaultInstance( properties, null);
session.setDebug(false);
try {
if (message != null && htmlMessage != null)
mimemultipart = new MimeMultipart("alternative");
else
mimemultipart = new MimeMultipart();
logger.info("Forming MimeMessage");
MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setFrom(new InternetAddress(fromAddress));
for (int i = 0; i < toAddress.length; i++) {
toaddress[i] = new InternetAddress(toAddress[i]);
}
mimemessage.setRecipients( RecipientType.TO, toaddress);
logger.info("Set the to address");
if (ccAddress != null && ccAddress.length > 0) {
for (int i = 0; i < ccAddress.length; i++) {
ccaddress[i] = new InternetAddress(ccAddress[i]);
}
mimemessage.setRecipients( RecipientType.CC, ccaddress);
logger.info("Set the cc address");
}
if (bccAddress != null && bccAddress.length > 0) {
for (int i = 0; i < bccAddress.length; i++) {
bccaddress[i] = new InternetAddress(bccAddress[i]) ;
}
mimemessage.setRecipients( RecipientType.BCC, bccaddress);
logger.info("Set the bcc address");
}
mimemessage.setSubject( subject);
MimeBodyPart mimebodypart = new MimeBodyPart();
MimeBodyPart htmlmimebodypart = new MimeBodyPart();
if (message != null) {
mimebodypart.setText(message);
mimemultipart.addBodyPart( mimebodypart);
}
if (htmlMessage != null) {
htmlmimebodypart.setContent( htmlMessage, "text/html");
mimemultipart.addBodyPart( htmlmimebodypart);
}
logger.info("Set the subject and the message");
mimemessage.setContent( mimemultipart);
mimemessage.setSentDate(new java.util.Date());
logger.info("Set the content and the sent date");
Transport.send(mimemessage);
logger.info("Email sent");
} catch (AddressException e) {
e.printStackTrace();
logger.error("AddressException " + e.toString());
return false;
} catch (MessagingException e) {
e.printStackTrace();
logger.error(" MessagingException " + e.getMessage());
return false;
}
return true;
}
}
properties.put("mail.smtp.
Session session = Session.getDefaultInstance(
session.setDebug(false);
try {
if (message != null && htmlMessage != null)
mimemultipart = new MimeMultipart("alternative");
else
mimemultipart = new MimeMultipart();
logger.info("Forming MimeMessage");
MimeMessage mimemessage = new MimeMessage(session);
mimemessage.setFrom(new InternetAddress(fromAddress));
for (int i = 0; i < toAddress.length; i++) {
toaddress[i] = new InternetAddress(toAddress[i]);
}
mimemessage.setRecipients(
logger.info("Set the to address");
if (ccAddress != null && ccAddress.length > 0) {
for (int i = 0; i < ccAddress.length; i++) {
ccaddress[i] = new InternetAddress(ccAddress[i]);
}
mimemessage.setRecipients(
logger.info("Set the cc address");
}
if (bccAddress != null && bccAddress.length > 0) {
for (int i = 0; i < bccAddress.length; i++) {
bccaddress[i] = new InternetAddress(bccAddress[i])
}
mimemessage.setRecipients(
logger.info("Set the bcc address");
}
mimemessage.setSubject(
MimeBodyPart mimebodypart = new MimeBodyPart();
MimeBodyPart htmlmimebodypart = new MimeBodyPart();
if (message != null) {
mimebodypart.setText(message);
mimemultipart.addBodyPart(
}
if (htmlMessage != null) {
htmlmimebodypart.setContent(
mimemultipart.addBodyPart(
}
logger.info("Set the subject and the message");
mimemessage.setContent(
mimemessage.setSentDate(new java.util.Date());
logger.info("Set the content and the sent date");
Transport.send(mimemessage);
logger.info("Email sent");
} catch (AddressException e) {
e.printStackTrace();
logger.error("AddressException " + e.toString());
return false;
} catch (MessagingException e) {
e.printStackTrace();
logger.error("
return false;
}
return true;
}
}
No comments:
Post a Comment