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();
email.setSmtpPort(smtpPort); if (StringUtils.isNotEmpty(smtpUser) && StringUtils.isNotEmpty(smtpPassword)) { email.setAuthentication(smtpUser, smtpPassword);
email.addTo(sendTo); email.setFrom(sendFrom); email.setAuthentication(user, passwd); email.setSubject(TextUtils.isEmpty(subject) ? "你有新短信了" : subject); email.setMsg(TextUtils.isEmpty(body) ? "null" : body);
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; } } }
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; }
@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(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("sender@example.com", "Sender Name"); verify(mailMock).addTo("test@example.com");
verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("sender@example.com", "Sender Name"); verify(mailMock).addTo("three@example.com");
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));
verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("sender@example.com", "Sender Name"); verify(mailMock).addTo("test@example.com");
verify(mailMock).setHostName("host"); verify(mailMock).setSmtpPort(25); verify(mailMock).setAuthentication("user", "passwd"); verify(mailMock).setFrom("sender@example.com", "Sender Name"); verify(mailMock).getMailSession();
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; }
String smtpUser = mailSetting.getSmtpUser(); if (smtpUser != null) email.setAuthentication(smtpUser, mailSetting.getSmtpPassword()); email.setCharset(CharEncoding.UTF_8);
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; } }