You are on page 1of 5

package com.moratel.util; import org.apache.log4j.Logger; import import import import import import import javax.mail.*; javax.mail.internet.InternetAddress; javax.mail.internet.MimeMessage; java.io.

UnsupportedEncodingException; java.util.ArrayList; java.util.List; java.util.Properties;

/** * Created with IntelliJ IDEA. * User: Doni Wahyu Shahri Romadhona */ public class MailUtil { static Logger logger = Logger.getLogger(MailUtil.class); // private static String HOST = "mail.moratelindo.co.id"; private static String HOST = "27.50.30.220"; // ip mail server private static String USER = "system-dms@moratelindo.co.id"; private static String PASSWORD = "moratel"; // private static String PORT = "465"; private static String PORT = "587"; private static String FROM = "system-dms@moratelindo.co.id"; private static String TO = "doni.wahyu@moratelindo.co.id"; private static class SMTPAuthenticator extends Authenticator { private PasswordAuthentication authentication; public SMTPAuthenticator(String login, String password) { authentication = new PasswordAuthentication(login, password); } @Override protected PasswordAuthentication getPasswordAuthentication() { return authentication; } } public static void main(String[] args) { List<String> listCc = new ArrayList<String>(); listCc.add("doni.wahyu@moratelindo.co.id"); listCc.add("doni.wahyu@gmail.com"); for (String s : listCc) { System.out.println("s = " + s.toUpperCase()); } // MailUtil mailUtil = new MailUtil(); String sContent = "<table border=1>" + "<tr><td>Column Header</td></tr>" + "<tr><td>Detail</td></tr>" + "</table>"; mailUtil.send("doez_entoz@yahoo.cvom", listCc, "Test CC mail", sConten

//

t); MailUtil.send("doni.wahyu@moratelindo.co.id", listCc, "Test CC mail", sC ontent); } public static void send(String sTo, List<String> sCc, String sSubject, Strin g sContent) { logger.info("start send email ==> [" + sTo + "/" + sCc.toString() + "/" + sSubject + "/" + sContent + "]"); Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.starttls.enable", "true"); properties.setProperty("mail.smtp.host", HOST ); properties.setProperty("mail.smtp.user", FROM); properties.setProperty("mail.smtp.password", PASSWORD); properties.setProperty("mail.smtp.port", PORT); properties.setProperty("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(FROM, PASSWORD); // Get the default Session object. Session session = Session.getDefaultInstance(properties, auth); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(USER, "Approval Document Workflo w - Moratelindo")); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(sTo)); for (String cc : sCc) { message.addRecipient(Message.RecipientType.CC, new InternetAddre ss(cc)); } // Set Subject: header field message.setSubject(sSubject); sContent += "\nRegards,\n" + "<a href='" + Constants.BASE_URL + "'>Approval Document Work flow - Moratelindo</a> \n" + "Nb : This email sent automatically by system, don't reply i t"; // Send the actual HTML message, as big as you like message.setContent(sContent.replaceAll("(\r\n|\n)", "<br />"), "text /html"); // Send message Transport.send(message); logger.info("Sent email successfully....");

} catch (MessagingException mex) { logger.error(mex); mex.printStackTrace(); } catch (UnsupportedEncodingException e) { logger.error(e); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } /*try { logger.info("start send email ==> [" + sTo + "/" + sCc.toString() + "/" + sSubject + "]"); // Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName(HOST); email.addTo(sTo); for (String cc : sCc) { email.addCc(cc); } email.setFrom(FROM, "Approval Document Workflow - Moratelindo"); email.setSmtpPort(Integer.parseInt(PORT)); email.setAuthenticator(new DefaultAuthenticator(USER, PASSWORD)); email.setSSLOnConnect(true); email.setSubject(sSubject);

//

// add footer email sContent += "\nRegards,\n" + "<a href='" + Constants.BASE_URL + "'>Approval Document Work flow - Moratelindo</a> \n" + "Nb : This email sent automatically by system, don't reply i t"; // set the html message email.setHtmlMsg(sContent.replaceAll("(\r\n|\n)", "<br />")); // "); // set the alternative message email.setTextMsg("Your email client does not support HTML messages

// send the email email.send(); logger.info("====== end send email ======"); } catch (EmailException e) { logger.error(e); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }*/ } public static void send(String sTo, String sSubject, String sContent) { logger.info("start send email ==> [" + sTo + "/" + sSubject + "/" + sCon tent + "]"); Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.starttls.enable", "true");

properties.setProperty("mail.smtp.host", HOST ); properties.setProperty("mail.smtp.user", FROM); properties.setProperty("mail.smtp.password", PASSWORD); properties.setProperty("mail.smtp.port", PORT); properties.setProperty("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator(FROM, PASSWORD); // Get the default Session object. Session session = Session.getDefaultInstance(properties, auth); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(USER, "Approval Document Workflo w - Moratelindo")); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(sTo)); // Set Subject: header field message.setSubject(sSubject); sContent += "\nRegards,\n" + "<a href='" + Constants.BASE_URL + "'>Approval Document Work flow - Moratelindo</a> \n" + "Nb : This email sent automatically by system, don't reply i t"; // Send the actual HTML message, as big as you like message.setContent(sContent.replaceAll("(\r\n|\n)", "<br />"), "text /html"); // Send message Transport.send(message); logger.info("Sent email successfully...."); } catch (MessagingException mex) { logger.error(mex); mex.printStackTrace(); } catch (UnsupportedEncodingException e) { logger.error(e); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } /*try { logger.info("start send email ==> [" + sTo + "/" + sSubject + "]"); // Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName(HOST); email.addTo(sTo); email.setFrom(FROM, "Approval Document Workflow - Moratelindo"); email.setSmtpPort(Integer.parseInt(PORT));

//

email.setAuthenticator(new DefaultAuthenticator(USER, PASSWORD)); email.setSSLOnConnect(true); email.setSubject(sSubject);

// add footer email sContent += "\nRegards,\n" + "<a href='" + Constants.BASE_URL + "'>Approval Document Work flow - Moratelindo</a> \n" + "Nb : This email sent automatically by system, don't reply i t"; // set the html message email.setHtmlMsg(sContent.replaceAll("(\r\n|\n)", "<br />")); // "); // set the alternative message email.setTextMsg("Your email client does not support HTML messages

// send the email email.send(); logger.info("====== end send email ======"); } catch (EmailException e) { logger.error(e); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }*/ } }