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 ImageHtmlEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("Test email with inline image"); // embed the image and get the content id URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg(htmlEmailTemplate, new File("").toURI().toURL(), false);
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); }
public HtmlEmail buildHtmlEmail(String emailAddress, String displayName) throws Exception { HashMap<String, String> configMap = acqConifMap(); String hostName = configMap.get(EmailItemEnum.EMAIL_HOST.getItemKey()); String userName = configMap.get(EmailItemEnum.EMAIL_USERNAME.getItemKey()); String userPwd = configMap.get(EmailItemEnum.EMAIL_PWD.getItemKey()); // Create the email message HtmlEmail email = new HtmlEmail(); email.setAuthentication(userName, userPwd); email.setHostName(hostName); email.addTo(emailAddress, displayName); email.setFrom("msg@51xianqu.net", "CMDB"); email.setSubject("工单消息~"); email.setCharset("UTF-8"); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); return email; }
HtmlEmail email = new HtmlEmail(); email.setSubject("<your subject>"); email.setHtmlMsg("<your html message body>"); email.setHostName("<host>"); email.setFrom("<from_address>"); email.addTo("<recipient_address>"); email.send();
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); }
public static void main(String[] args) throws EmailException, MalformedURLException { // 创建 Email Message HtmlEmail email = new HtmlEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("Test email with inline image"); // 嵌入图片 URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // 发送 HTML 内容 email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // 设置替代消息 email.setTextMsg("Your email client does not support HTML messages"); // 发送 email.send(); } }
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();
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");
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; } }