/** * Sends a HTML email using the SMTP configuration in application.conf * * For more information @see <a href="https://commons.apache.org/proper/commons-email/userguide.html">https://commons.apache.org/proper/commons-email/userguide.html</a> * * @param htmlEmail The HTML email to send */ public void send(HtmlEmail htmlEmail) { htmlEmail.setHostName(this.host); htmlEmail.setSmtpPort(this.port); htmlEmail.setAuthenticator(this.defaultAuthenticator); htmlEmail.setSSLOnConnect(this.ssl); try { htmlEmail.send(); } catch (EmailException e) { LOG.error("Failed to send HtmlEmail", e); } } }
email.setAuthenticator(new DefaultAuthenticator(mailServerUsername, mailServerPassword)); email.setSSLOnConnect(mailServerSSLOnConnect); try {
private HtmlEmail getHtmlEmail( String hostName, int port, String username, String password, boolean tls, String sender ) throws EmailException { HtmlEmail email = new HtmlEmail(); email.setHostName( hostName ); email.setFrom( sender, getEmailName() ); email.setSmtpPort( port ); email.setStartTLSEnabled( tls ); if ( username != null && password != null ) { email.setAuthenticator( new DefaultAuthenticator( username, password ) ); } return email; }
HtmlEmail email = new HtmlEmail(); String authuser = "user"; String authpwd = "pass"; email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd)); email.setHostName("smtp.gmail.com"); // properties to configure encryption email.getMailSession().getProperties().put("mail.smtps.auth", "true"); email.getMailSession().getProperties().put("mail.debug", "true"); email.getMailSession().getProperties().put("mail.smtps.port", "587"); email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587"); email.getMailSession().getProperties().put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false"); email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setHostName("smtp.gmail.com"); htmlEmail.setSmtpPort(587); htmlEmail.setDebug(true); htmlEmail.setAuthenticator(new DefaultAuthenticator("userId", "password")); htmlEmail.setTLS(true); htmlEmail.addTo("recipient@gmail.com", "recipient"); htmlEmail.setFrom("sender@gmail.com", "sender"); htmlEmail.setSubject("Send HTML email with body content from URI"); htmlEmail.setHtmlMsg(responseBody); htmlEmail.send();
private static void initializeEmail(EmailSettings emailSettings) throws EmailException { _logger.info("{}", emailSettings); email = new HtmlEmail(); email.setHostName(emailSettings.getSmtpHost()); email.setSmtpPort(emailSettings.getSmtpPort()); if (emailSettings.getSmtpUsername() != null && emailSettings.getSmtpUsername().length() > 0) { email.setAuthenticator(new DefaultAuthenticator( emailSettings.getSmtpUsername(), emailSettings.getSmtpPassword())); } if (emailSettings.getEnableSsl()) { if (emailSettings.getUseStartTLS()) { email.setStartTLSEnabled(emailSettings.getEnableSsl()); } else { email.setSSLOnConnect(emailSettings.getEnableSsl()); } } email.setFrom(emailSettings.getFromAddress()); }
private static void initializeEmail(EmailSettings emailSettings) throws EmailException { _logger.info("{}", emailSettings); email = new HtmlEmail(); email.setHostName(emailSettings.getSmtpHost()); email.setSmtpPort(emailSettings.getSmtpPort()); if (emailSettings.getSmtpUsername() != null && emailSettings.getSmtpUsername().length() > 0) { email.setAuthenticator(new DefaultAuthenticator( emailSettings.getSmtpUsername(), emailSettings.getSmtpPassword())); } if (emailSettings.getEnableSsl()) { if (emailSettings.getUseStartTLS()) { email.setStartTLSEnabled(emailSettings.getEnableSsl()); } else { email.setSSLOnConnect(emailSettings.getEnableSsl()); } } email.setFrom(emailSettings.getFromAddress()); }
import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import play.mvc.Controller; import play.mvc.Result; public class MailController extends Controller { public Result sendEmail() throws EmailException { HtmlEmail email = new HtmlEmail(); String authuser = "..........@gmail.com"; String authpwd = "XXXXXX"; email.setSmtpPort(587); email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd)); email.setDebug(true); email.setHostName("smtp.gmail.com"); email.setFrom("from@gmail.com", "SenderName"); email.setSubject("TestMail"); email.setHtmlMsg("<html><body><h1>welcome to u</h1></body></html>"); email.addTo("to@gmail.com", "receiver name"); email.setTLS(true); email.send(); return play.mvc.Results.ok("Success"); } }
email.setAuthenticator(new DefaultAuthenticator("youremail@yahoo.com", "yourpasswordhere"));