HtmlEmail email = new HtmlEmail(); email.setHostName(mailserver); email.setAuthentication(username, password); email.setSmtpPort(port); email.setFrom(fromEmail); email.addTo(to); email.setSubject(subject); email.setTextMsg(textBody); email.setHtmlMsg(htmlBody); email.setDebug(true); email.send();
HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.gmail.com"); email.setSmtpPort(465); email.setSSL(true);
/** * 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); } } }
private void createAndSendAlertEmail(String body) { try { log.info(String.format("Sending alert email to [%s]: %s", config.getRecipients(), body)); HtmlEmail email = new HtmlEmail(); email.setTextMsg(body); email.setFrom("esper-is-awesome@example.com"); email.setTo(Arrays.asList(new InternetAddress(config.getRecipients()))); email.setHostName(config.getHost()); email.setSmtpPort(config.getPort()); email.send(); } catch (Exception ex) { log.warn("Could not create or send email", ex); } } }
private boolean send(String title, String content, List<String> receivers) { try { HtmlEmail email = new HtmlEmail(); email.setAuthentication(Configuration.MAIL_SENDER_USERNAME, Configuration.MAIL_SENDER_PASSWORD); email.setHostName(Configuration.MAIL_SENDER_SMTP_HOST); email.setSmtpPort(Configuration.MAIL_SENDER_SMTP_PORT); email.setFrom(Configuration.MAIL_SENDER_USERNAME, Configuration.MAIL_SENDER_NAME); email.setSubject(title); email.setHtmlMsg(content); email.addTo(receivers.stream().toArray(String[]::new)); email.setCharset("UTF-8"); email.setSSLOnConnect(false); email.send(); logger.info("send mail to {} success", JSON.toJSONString(receivers)); return true; } catch (Exception e) { logger.error("send mail to {} fail, caused by", JSON.toJSONString(receivers), e); return false; } } }
@Test public void badSmtpPort() throws Exception { doThrow(IllegalArgumentException.class).when(mailMock).setSmtpPort(-1); when(objectFactoryMock.createHtmlEmail()).thenReturn(mailMock); when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.smtpHost = "host"; mailSender.smtpPort = -1; mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.sendEMail("subject", "htmlBody", "textBody", Arrays.asList("one@example.com")); assertThat(result, is(false)); verify(objectFactoryMock).getSmtpTransport(); verify(objectFactoryMock).createHtmlEmail(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verify(transportMock).close(); verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(-1); verifyNoMoreInteractions(objectFactoryMock, transportMock, mailMock); }
@Test public void badSenderAdress() throws Exception { when(objectFactoryMock.createHtmlEmail()).thenReturn(mailMock); when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(EmailException.class).when(mailMock).setFrom(eq("invalid"), any(String.class)); mailSender.defaultRecipientString = "one@example.com,two@example.com,invalid"; mailSender.smtpPropertiesString = "key1=val1,key2=val2,=noKey,noVal=,=,invalid"; mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.senderAddress = "invalid"; mailSender.senderName = "Sender Name"; mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.sendEMail("subject", "htmlBody", "textBody", Arrays.asList("three@example.com")); assertThat(result, is(false)); verify(objectFactoryMock).getSmtpTransport(); verify(objectFactoryMock).createHtmlEmail(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verify(transportMock).close(); verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("invalid", "Sender Name"); verifyNoMoreInteractions(objectFactoryMock, transportMock, mailMock); }
ApacheCommonsEmail(String hostname, int smtpPort, boolean useSsl, boolean useTls, String user, String password, HtmlEmail wrappedEmail) { this.wrappedEmail = wrappedEmail; this.wrappedEmail.setHostName(hostname); this.wrappedEmail.setSmtpPort(smtpPort); this.wrappedEmail.setSSLOnConnect(useSsl); this.wrappedEmail.setStartTLSEnabled(useTls); if (!isNullOrEmpty(user) && !isNullOrEmpty(password)) { this.wrappedEmail.setAuthentication(user, password); } this.wrappedEmail.setDebug(false); }
verify(transportMock).close(); verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("sender@example.com", "Sender Name");
verify(transportMock).close(); verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("sender@example.com", "Sender Name");
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; }
private HtmlEmail createNewEmail(final SessionInfo session) throws EmailException { final HtmlEmail email = new HtmlEmail(); email.setCharset(Defaults.CHARSET.displayName()); // otherwise the e-mail contents are mangled email.setHostName(getSmtpHostname()); email.setSmtpPort(getSmtpPort()); email.setStartTLSRequired(isStartTlsRequired()); email.setSSLOnConnect(isSslOnConnectRequired()); if (isAuthenticationRequired()) { final String username = getSmtpUsername(); LOGGER.debug("Will contact SMTP server as '{}'.", username); email.setAuthentication(getSmtpUsername(), getSmtpPassword()); } else { LOGGER.debug("Will contact SMTP server anonymously."); } email.setFrom(getSender(), session.getName()); email.addTo(getRecipient()); return email; }
private HtmlEmail createNewEmail(final SessionInfo session) throws EmailException { final HtmlEmail email = new HtmlEmail(); email.setCharset(Defaults.CHARSET.displayName()); // otherwise the e-mail contents are mangled email.setHostName(getSmtpHostname()); email.setSmtpPort(getSmtpPort()); email.setStartTLSRequired(isStartTlsRequired()); email.setSSLOnConnect(isSslOnConnectRequired()); if (isAuthenticationRequired()) { final String username = getSmtpUsername(); LOGGER.debug("Will contact SMTP server as '{}'.", username); email.setAuthentication(getSmtpUsername(), getSmtpPassword()); } else { LOGGER.debug("Will contact SMTP server anonymously."); } email.setFrom(getSender(), session.getName()); email.addTo(getRecipient()); return email; }
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 HtmlEmail createDefaultHtmlEmail() throws EmailException { final HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setHostName(this.strTestMailServer); htmlEmail.setSmtpPort(this.getMailServerPort()); htmlEmail.setFrom("a@b.com"); htmlEmail.addTo("c@d.com"); return htmlEmail; } }
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()); }
public void sendHtmlEmail(String toEmail, String toName, String subject, String htmlMsg, String senderMail, String senderName, String mail_userName, String mail_password, String smtpPort) throws Exception { try { HtmlEmail email = new HtmlEmail(); MailServerInfo msi = (MailServerInfo)serverinfos.get(this.getMailDomain(senderMail)); String hostName = msi.getSmtpServer(); String sender_Mail = senderMail == null ? msi.getMailSender() : senderMail; String sender_Name = senderName == null ? msi.getMailSenderName() : senderName; String mailUserName = mail_userName == null ? msi.getMail_userName() : mail_userName; String mailPassword = mail_password == null ? msi.getMail_password() : mail_password; email.setAuthentication(mailUserName, mailPassword); email.setHostName(hostName); email.setSmtpPort(Integer.parseInt(smtpPort)); email.addTo(toEmail, toName); email.setFrom(sender_Mail, sender_Name); email.setSubject(subject); // email.setContent("HtmlEmail", "text/plain; charset=utf-8"); email.setHtmlMsg(htmlMsg); email.setTextMsg("Your email client does not support HTML messages"); email.setDebug(true); email.send(); } catch (EmailException e) { e.printStackTrace(); throw e; } }
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.setSmtpPort(587);