0% found this document useful (0 votes)
24 views5 pages

Java SMTP Email Sending Utility

The document defines a MailUtil class for sending emails from a Java application. It contains methods for sending emails with and without CC recipients. The methods setup the mail server properties, create a MimeMessage, set the from, to, cc addresses and subject, add the email body content, and send the message using JavaMail API classes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Java SMTP Email Sending Utility

The document defines a MailUtil class for sending emails from a Java application. It contains methods for sending emails with and without CC recipients. The methods setup the mail server properties, create a MimeMessage, set the from, to, cc addresses and subject, add the email body content, and send the message using JavaMail API classes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package [Link]; import [Link]; import import import import import import import [Link].*; [Link]; [Link]; [Link].

UnsupportedEncodingException; [Link]; [Link]; [Link];

/** * Created with IntelliJ IDEA. * User: Doni Wahyu Shahri Romadhona */ public class MailUtil { static Logger logger = [Link]([Link]); // private static String HOST = "[Link]"; private static String HOST = "[Link]"; // ip mail server private static String USER = "system-dms@[Link]"; private static String PASSWORD = "moratel"; // private static String PORT = "465"; private static String PORT = "587"; private static String FROM = "system-dms@[Link]"; private static String TO = "[Link]@[Link]"; 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>(); [Link]("[Link]@[Link]"); [Link]("[Link]@[Link]"); for (String s : listCc) { [Link]("s = " + [Link]()); } // MailUtil mailUtil = new MailUtil(); String sContent = "<table border=1>" + "<tr><td>Column Header</td></tr>" + "<tr><td>Detail</td></tr>" + "</table>"; [Link]("doez_entoz@[Link]", listCc, "Test CC mail", sConten

//

t); [Link]("[Link]@[Link]", listCc, "Test CC mail", sC ontent); } public static void send(String sTo, List<String> sCc, String sSubject, Strin g sContent) { [Link]("start send email ==> [" + sTo + "/" + [Link]() + "/" + sSubject + "/" + sContent + "]"); Properties properties = [Link](); // Setup mail server [Link]("[Link]", "true"); [Link]("[Link]", HOST ); [Link]("[Link]", FROM); [Link]("[Link]", PASSWORD); [Link]("[Link]", PORT); [Link]("[Link]", "true"); Authenticator auth = new SMTPAuthenticator(FROM, PASSWORD); // Get the default Session object. Session session = [Link](properties, auth); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. [Link](new InternetAddress(USER, "Approval Document Workflo w - Moratelindo")); // Set To: header field of the header. [Link]([Link], new InternetAddress(sTo)); for (String cc : sCc) { [Link]([Link], new InternetAddre ss(cc)); } // Set Subject: header field [Link](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 [Link]([Link]("(\r\n|\n)", "<br />"), "text /html"); // Send message [Link](message); [Link]("Sent email successfully....");

} catch (MessagingException mex) { [Link](mex); [Link](); } catch (UnsupportedEncodingException e) { [Link](e); [Link](); //To change body of catch statement use File | Settings | File Templates. } /*try { [Link]("start send email ==> [" + sTo + "/" + [Link]() + "/" + sSubject + "]"); // Create the email message HtmlEmail email = new HtmlEmail(); [Link](HOST); [Link](sTo); for (String cc : sCc) { [Link](cc); } [Link](FROM, "Approval Document Workflow - Moratelindo"); [Link]([Link](PORT)); [Link](new DefaultAuthenticator(USER, PASSWORD)); [Link](true); [Link](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 [Link]([Link]("(\r\n|\n)", "<br />")); // "); // set the alternative message [Link]("Your email client does not support HTML messages

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

[Link]("[Link]", HOST ); [Link]("[Link]", FROM); [Link]("[Link]", PASSWORD); [Link]("[Link]", PORT); [Link]("[Link]", "true");

Authenticator auth = new SMTPAuthenticator(FROM, PASSWORD); // Get the default Session object. Session session = [Link](properties, auth); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. [Link](new InternetAddress(USER, "Approval Document Workflo w - Moratelindo")); // Set To: header field of the header. [Link]([Link], new InternetAddress(sTo)); // Set Subject: header field [Link](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 [Link]([Link]("(\r\n|\n)", "<br />"), "text /html"); // Send message [Link](message); [Link]("Sent email successfully...."); } catch (MessagingException mex) { [Link](mex); [Link](); } catch (UnsupportedEncodingException e) { [Link](e); [Link](); //To change body of catch statement use File | Settings | File Templates. } /*try { [Link]("start send email ==> [" + sTo + "/" + sSubject + "]"); // Create the email message HtmlEmail email = new HtmlEmail(); [Link](HOST); [Link](sTo); [Link](FROM, "Approval Document Workflow - Moratelindo"); [Link]([Link](PORT));

//

[Link](new DefaultAuthenticator(USER, PASSWORD)); [Link](true); [Link](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 [Link]([Link]("(\r\n|\n)", "<br />")); // "); // set the alternative message [Link]("Your email client does not support HTML messages

// send the email [Link](); [Link]("====== end send email ======"); } catch (EmailException e) { [Link](e); [Link](); //To change body of catch statement use File | Settings | File Templates. }*/ } }

You might also like