List of usage examples for org.apache.commons.mail Email setStartTLSEnabled
public Email setStartTLSEnabled(final boolean startTlsEnabled)
From source file:org.activiti5.engine.impl.bpmn.behavior.MailActivityBehavior.java
protected void setMailServerProperties(Email email, String tenantId) { ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration(); boolean isMailServerSet = false; if (tenantId != null && tenantId.length() > 0) { if (processEngineConfiguration.getMailSessionJndi(tenantId) != null) { setEmailSession(email, processEngineConfiguration.getMailSessionJndi(tenantId)); isMailServerSet = true;// w w w .j ava2 s . c om } else if (processEngineConfiguration.getMailServer(tenantId) != null) { MailServerInfo mailServerInfo = processEngineConfiguration.getMailServer(tenantId); String host = mailServerInfo.getMailServerHost(); if (host == null) { throw new ActivitiException( "Could not send email: no SMTP host is configured for tenantId " + tenantId); } email.setHostName(host); email.setSmtpPort(mailServerInfo.getMailServerPort()); email.setSSLOnConnect(processEngineConfiguration.getMailServerUseSSL()); email.setStartTLSEnabled(processEngineConfiguration.getMailServerUseTLS()); String user = mailServerInfo.getMailServerUsername(); String password = mailServerInfo.getMailServerPassword(); if (user != null && password != null) { email.setAuthentication(user, password); } isMailServerSet = true; } } if (!isMailServerSet) { String mailSessionJndi = processEngineConfiguration.getMailSessionJndi(); if (mailSessionJndi != null) { setEmailSession(email, mailSessionJndi); } else { String host = processEngineConfiguration.getMailServerHost(); if (host == null) { throw new ActivitiException("Could not send email: no SMTP host is configured"); } email.setHostName(host); int port = processEngineConfiguration.getMailServerPort(); email.setSmtpPort(port); email.setSSLOnConnect(processEngineConfiguration.getMailServerUseSSL()); email.setStartTLSEnabled(processEngineConfiguration.getMailServerUseTLS()); String user = processEngineConfiguration.getMailServerUsername(); String password = processEngineConfiguration.getMailServerPassword(); if (user != null && password != null) { email.setAuthentication(user, password); } } } }
From source file:org.apache.kylin.common.util.MailService.java
/** * @param receivers//from w w w .j a v a2 s . c om * @param subject * @param content * @return true or false indicating whether the email was delivered successfully * @throws IOException */ public boolean sendMail(List<String> receivers, String subject, String content, boolean isHtmlMsg) { if (!enabled) { logger.info("Email service is disabled; this mail will not be delivered: " + subject); logger.info("To enable mail service, set 'kylin.job.notification-enabled=true' in kylin.properties"); return false; } Email email = new HtmlEmail(); email.setHostName(host); email.setStartTLSEnabled(starttlsEnabled); if (starttlsEnabled) { email.setSslSmtpPort(port); } else { email.setSmtpPort(Integer.valueOf(port)); } if (username != null && username.trim().length() > 0) { email.setAuthentication(username, password); } //email.setDebug(true); try { for (String receiver : receivers) { email.addTo(receiver); } email.setFrom(sender); email.setSubject(subject); email.setCharset("UTF-8"); if (isHtmlMsg) { ((HtmlEmail) email).setHtmlMsg(content); } else { ((HtmlEmail) email).setTextMsg(content); } email.send(); email.getMailSession(); } catch (EmailException e) { logger.error(e.getLocalizedMessage(), e); return false; } return true; }
From source file:org.apache.nifi.processors.email.TestListenSMTP.java
@Test public void validateSuccessfulInteractionWithTls() throws Exception, EmailException { System.setProperty("mail.smtp.ssl.trust", "*"); System.setProperty("javax.net.ssl.keyStore", "src/test/resources/localhost-ks.jks"); System.setProperty("javax.net.ssl.keyStorePassword", "localtest"); int port = NetworkUtils.availablePort(); TestRunner runner = TestRunners.newTestRunner(ListenSMTP.class); runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port)); runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3"); runner.setProperty(ListenSMTP.SMTP_TIMEOUT, "10 seconds"); // Setup the SSL Context SSLContextService sslContextService = new StandardSSLContextService(); runner.addControllerService("ssl-context", sslContextService); runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE, "src/test/resources/localhost-ts.jks"); runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_PASSWORD, "localtest"); runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_TYPE, "JKS"); runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE, "src/test/resources/localhost-ks.jks"); runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_PASSWORD, "localtest"); runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_TYPE, "JKS"); runner.enableControllerService(sslContextService); // and add the SSL context to the runner runner.setProperty(ListenSMTP.SSL_CONTEXT_SERVICE, "ssl-context"); runner.setProperty(ListenSMTP.CLIENT_AUTH, SSLContextService.ClientAuth.NONE.name()); runner.assertValid();// ww w .j a v a 2s .c o m int messageCount = 5; CountDownLatch latch = new CountDownLatch(messageCount); runner.run(messageCount, false); this.executor.schedule(new Runnable() { @Override public void run() { for (int i = 0; i < messageCount; i++) { try { Email email = new SimpleEmail(); email.setHostName("localhost"); email.setSmtpPort(port); email.setFrom("alice@nifi.apache.org"); email.setSubject("This is a test"); email.setMsg("MSG-" + i); email.addTo("bob@nifi.apache.org"); // Enable STARTTLS but ignore the cert email.setStartTLSEnabled(true); email.setStartTLSRequired(true); email.setSSLCheckServerIdentity(false); email.send(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { latch.countDown(); } } } }, 1500, TimeUnit.MILLISECONDS); boolean complete = latch.await(5000, TimeUnit.MILLISECONDS); runner.shutdown(); assertTrue(complete); runner.assertAllFlowFilesTransferred("success", messageCount); }
From source file:org.fao.geonet.util.MailUtil.java
/** * Create data information to compose the mail * * @param hostName//from w w w .j a v a2 s.c o m * @param smtpPort * @param from * @param username * @param password * @param email * @param ssl * @param tls * @param ignoreSslCertificateErrors */ private static void configureBasics(String hostName, Integer smtpPort, String from, String username, String password, Email email, Boolean ssl, Boolean tls, Boolean ignoreSslCertificateErrors) { if (hostName != null) { email.setHostName(hostName); } else { throw new IllegalArgumentException( "Missing settings in System Configuration (see Administration menu) - cannot send mail"); } if (StringUtils.isNotBlank(smtpPort + "")) { email.setSmtpPort(smtpPort); } else { throw new IllegalArgumentException( "Missing settings in System Configuration (see Administration menu) - cannot send mail"); } if (username != null) { email.setAuthenticator(new DefaultAuthenticator(username, password)); } email.setDebug(true); if (tls != null && tls) { email.setStartTLSEnabled(tls); email.setStartTLSRequired(tls); } if (ssl != null && ssl) { email.setSSLOnConnect(ssl); if (StringUtils.isNotBlank(smtpPort + "")) { email.setSslSmtpPort(smtpPort + ""); } } if (ignoreSslCertificateErrors != null && ignoreSslCertificateErrors) { try { Session mailSession = email.getMailSession(); Properties p = mailSession.getProperties(); p.setProperty("mail.smtp.ssl.trust", "*"); } catch (EmailException e) { // Ignore the exception. Can't be reached because the host name is always set above or an // IllegalArgumentException is thrown. } } if (StringUtils.isNotBlank(from)) { try { email.setFrom(from); } catch (EmailException e) { throw new IllegalArgumentException( "Invalid 'from' email setting in System Configuration (see Administration menu) - cannot send " + "mail", e); } } else { throw new IllegalArgumentException( "Missing settings in System Configuration (see Administration menu) - cannot send mail"); } }
From source file:org.flowable.engine.impl.bpmn.behavior.MailActivityBehavior.java
protected void setMailServerProperties(Email email, String tenantId) { ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration(); boolean isMailServerSet = false; if (tenantId != null && tenantId.length() > 0) { if (processEngineConfiguration.getMailSessionJndi(tenantId) != null) { setEmailSession(email, processEngineConfiguration.getMailSessionJndi(tenantId)); isMailServerSet = true;/*w w w .ja v a 2 s .c om*/ } else if (processEngineConfiguration.getMailServer(tenantId) != null) { MailServerInfo mailServerInfo = processEngineConfiguration.getMailServer(tenantId); String host = mailServerInfo.getMailServerHost(); if (host == null) { throw new FlowableException( "Could not send email: no SMTP host is configured for tenantId " + tenantId); } email.setHostName(host); email.setSmtpPort(mailServerInfo.getMailServerPort()); email.setSSLOnConnect(mailServerInfo.isMailServerUseSSL()); email.setStartTLSEnabled(mailServerInfo.isMailServerUseTLS()); String user = mailServerInfo.getMailServerUsername(); String password = mailServerInfo.getMailServerPassword(); if (user != null && password != null) { email.setAuthentication(user, password); } isMailServerSet = true; } } if (!isMailServerSet) { String mailSessionJndi = processEngineConfiguration.getMailSessionJndi(); if (mailSessionJndi != null) { setEmailSession(email, mailSessionJndi); } else { String host = processEngineConfiguration.getMailServerHost(); if (host == null) { throw new FlowableException("Could not send email: no SMTP host is configured"); } email.setHostName(host); int port = processEngineConfiguration.getMailServerPort(); email.setSmtpPort(port); email.setSSLOnConnect(processEngineConfiguration.getMailServerUseSSL()); email.setStartTLSEnabled(processEngineConfiguration.getMailServerUseTLS()); String user = processEngineConfiguration.getMailServerUsername(); String password = processEngineConfiguration.getMailServerPassword(); if (user != null && password != null) { email.setAuthentication(user, password); } } } }
From source file:org.graylog2.alerts.FormattedEmailAlertSender.java
private void sendEmail(String emailAddress, Stream stream, AlertCondition.CheckResult checkResult, List<Message> backlog) throws TransportConfigurationException, EmailException { LOG.debug("Sending mail to " + emailAddress); if (!configuration.isEnabled()) { throw new TransportConfigurationException( "Email transport is not enabled in server configuration file!"); }/*from ww w . j ava2s . c o m*/ final Email email = new SimpleEmail(); email.setCharset(EmailConstants.UTF_8); if (isNullOrEmpty(configuration.getHostname())) { throw new TransportConfigurationException( "No hostname configured for email transport while trying to send alert email!"); } else { email.setHostName(configuration.getHostname()); } email.setSmtpPort(configuration.getPort()); if (configuration.isUseSsl()) { email.setSslSmtpPort(Integer.toString(configuration.getPort())); } if (configuration.isUseAuth()) { email.setAuthenticator(new DefaultAuthenticator(Strings.nullToEmpty(configuration.getUsername()), Strings.nullToEmpty(configuration.getPassword()))); } email.setSSLOnConnect(configuration.isUseSsl()); email.setStartTLSEnabled(configuration.isUseTls()); if (pluginConfig != null && !isNullOrEmpty(pluginConfig.getString("sender"))) { email.setFrom(pluginConfig.getString("sender")); } else { email.setFrom(configuration.getFromEmail()); } email.setSubject(buildSubject(stream, checkResult, backlog)); email.setMsg(buildBody(stream, checkResult, backlog)); email.addTo(emailAddress); email.send(); }
From source file:org.graylog2.alerts.StaticEmailAlertSender.java
private void sendEmail(String emailAddress, Stream stream, AlertCondition.CheckResult checkResult, List<Message> backlog) throws TransportConfigurationException, EmailException { LOG.debug("Sending mail to " + emailAddress); if (!configuration.isEnabled()) { throw new TransportConfigurationException( "Email transport is not enabled in server configuration file!"); }// w w w. j av a 2 s . c o m final Email email = new SimpleEmail(); email.setCharset(EmailConstants.UTF_8); if (Strings.isNullOrEmpty(configuration.getHostname())) { throw new TransportConfigurationException( "No hostname configured for email transport while trying to send alert email!"); } else { email.setHostName(configuration.getHostname()); } email.setSmtpPort(configuration.getPort()); if (configuration.isUseSsl()) { email.setSslSmtpPort(Integer.toString(configuration.getPort())); } if (configuration.isUseAuth()) { email.setAuthenticator(new DefaultAuthenticator(Strings.nullToEmpty(configuration.getUsername()), Strings.nullToEmpty(configuration.getPassword()))); } email.setSSLOnConnect(configuration.isUseSsl()); email.setStartTLSEnabled(configuration.isUseTls()); if (pluginConfig != null && !Strings.isNullOrEmpty(pluginConfig.getString("sender"))) { email.setFrom(pluginConfig.getString("sender")); } else { email.setFrom(configuration.getFromEmail()); } email.setSubject(buildSubject(stream, checkResult, backlog)); email.setMsg(buildBody(stream, checkResult, backlog)); email.addTo(emailAddress); email.send(); }
From source file:org.mifosplatform.infrastructure.core.service.GmailPlatformEmailService.java
@Override public void sendToUserAccount(final EmailDetail emailDetail, final String unencodedPassword) { final Email email = new SimpleEmail(); // Very Important, Don't use email.setAuthentication() email.setAuthenticator(//from w w w .java 2 s. c o m new DefaultAuthenticator(credentials.getAuthUsername(), credentials.getAuthPassword())); email.setDebug(false); // true if you want to debug email.setHostName("smtp.gmail.com"); email.setSmtpPort(credentials.getSmtpPort()); try { email.setStartTLSRequired(true); email.setStartTLSEnabled(credentials.isStartTls()); email.setFrom(credentials.getAuthUsername(), credentials.getAuthUsername()); final StringBuilder subjectBuilder = new StringBuilder().append("FINEM U Ltd.: ") .append(emailDetail.getContactName()).append(" user account creation."); email.setSubject(subjectBuilder.toString()); final String sendToEmail = emailDetail.getAddress(); final StringBuilder messageBuilder = new StringBuilder() .append("You are receiving this email as your email account: ").append(sendToEmail) .append(" has being used to create a user account for an organisation named [") .append(emailDetail.getOrganisationName()).append("].") .append("You can login using the following credentials: username: ") .append(emailDetail.getUsername()).append(" password: ").append(unencodedPassword); email.setMsg(messageBuilder.toString()); email.addTo(sendToEmail, emailDetail.getContactName()); email.send(); } catch (final EmailException e) { throw new PlatformEmailSendException(e); } }
From source file:org.mifosplatform.infrastructure.security.service.JpaPlatformUserLoginFailureService.java
private void notify(String username, Integer failures) { GlobalConfigurationProperty property = globalConfigurationRepository.findOneByName("login-failure-limit"); Long limit = 3l;/*from w w w. j a v a 2s .c om*/ if (property != null && property.isEnabled() && property.getValue() != null) { limit = property.getValue(); } // NOTE: only send the email once if (failures == limit.intValue()) { lock(username); try { StringBuilder message = new StringBuilder(); message.append(String.format(template, limit)); final Email email = new SimpleEmail(); EmailCredentialsData credentials = getCredentials(); email.setAuthenticator( new DefaultAuthenticator(credentials.getAuthUsername(), credentials.getAuthPassword())); email.setDebug(credentials.isDebug()); email.setHostName(credentials.getHost()); email.setSmtpPort(credentials.getSmtpPort()); email.setStartTLSRequired(true); email.setStartTLSEnabled(credentials.isStartTls()); email.getMailSession().getProperties().put("mail.smtp.auth", true); email.setFrom(credentials.getAuthUsername(), credentials.getSenderName()); email.setSubject(subject); email.setMsg(message.toString()); email.addTo(appUserRepository.getEmailByUsername(username)); email.send(); } catch (Exception e) { logger.warn(e.toString(), e); } throw new LockedException( "User " + username + " has been locked after " + limit + " failed login attempts."); } }
From source file:org.opencms.mail.CmsMailUtil.java
/** * Configures the mail from the given mail host configuration data.<p> * * @param host the mail host configuration * @param mail the email instance//from w w w. ja va2s . co m */ public static void configureMail(CmsMailHost host, Email mail) { // set the host to the default mail host mail.setHostName(host.getHostname()); mail.setSmtpPort(host.getPort()); // check if username and password are provided String userName = host.getUsername(); if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userName)) { // authentication needed, set user name and password mail.setAuthentication(userName, host.getPassword()); } String security = host.getSecurity() != null ? host.getSecurity().trim() : null; if (SECURITY_SSL.equalsIgnoreCase(security)) { mail.setSslSmtpPort("" + host.getPort()); mail.setSSLOnConnect(true); } else if (SECURITY_STARTTLS.equalsIgnoreCase(security)) { mail.setStartTLSEnabled(true); } try { // set default mail from address mail.setFrom(OpenCms.getSystemInfo().getMailSettings().getMailFromDefault()); } catch (EmailException e) { // default email address is not valid, log error LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_SENDER_ADDRESS_0), e); } }