package com.mail; import javax.mail.Authenticator; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; public class ApacheHtmlEmail { public static void main (String[] args) { System.out.println("starts"); String userName = "YourGmailAccount@gmail.com"; String password = "YourGmailAccountPassword"; HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.gmail.com"); email.setSmtpPort(587); email.setTLS(true); Authenticator authenticator = new DefaultAuthenticator(userName, password); email.setAuthenticator(authenticator); try { email.setFrom("YourGmailAccount@gmail.com"); email.addTo("ToAddress@AnyProvider"); email.setSubject("Subject"); email.setTextMsg("Body of the message"); email.send(); } catch (EmailException e) { e.printStackTrace(); } System.out.println("Ends"); } }
Tuesday, December 20, 2011
Send mail From Java using Gmail Server as a SMTP server
Labels:
Java
Subscribe to:
Posts (Atom)