List of usage examples for org.apache.commons.mail Email setSslSmtpPort
public void setSslSmtpPort(final String sslSmtpPort)
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!"); }//from www . ja v a 2 s . co 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.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// ww w .j a v a2 s . c o 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); } }
From source file:org.openhab.binding.mail.internal.SMTPHandler.java
/** * use this server to send a mail//ww w . j ava2 s.c om * * @param mail the Email that needs to be sent * @return true if successful, false if failed */ public boolean sendMail(Email mail) { try { if (mail.getFromAddress() == null) { mail.setFrom(config.sender); } mail.setHostName(config.hostname); switch (config.security) { case SSL: mail.setSSLOnConnect(true); mail.setSslSmtpPort(config.port.toString()); break; case TLS: mail.setStartTLSEnabled(true); mail.setStartTLSRequired(true); mail.setSmtpPort(config.port); break; case PLAIN: mail.setSmtpPort(config.port); } if (!config.username.isEmpty() && !config.password.isEmpty()) { mail.setAuthenticator(new DefaultAuthenticator(config.username, config.password)); } mail.send(); } catch (EmailException e) { logger.warn("Trying to send mail but exception occured: {} ", e.getMessage()); return false; } return true; }
From source file:org.paxml.bean.EmailTag.java
private Email createEmail(Collection<String> to, Collection<String> cc, Collection<String> bcc) throws EmailException { Email email; if (attachment == null || attachment.isEmpty()) { email = new SimpleEmail(); } else {/*from ww w . j a va 2 s . c om*/ MultiPartEmail mpemail = new MultiPartEmail(); for (Object att : attachment) { mpemail.attach(makeAttachment(att.toString())); } email = mpemail; } if (StringUtils.isNotEmpty(username)) { String pwd = null; if (password instanceof Secret) { pwd = ((Secret) password).getDecrypted(); } else if (password != null) { pwd = password.toString(); } email.setAuthenticator(new DefaultAuthenticator(username, pwd)); } email.setHostName(findHost()); email.setSSLOnConnect(ssl); if (port > 0) { if (ssl) { email.setSslSmtpPort(port + ""); } else { email.setSmtpPort(port); } } if (replyTo != null) { for (Object r : replyTo) { email.addReplyTo(r.toString()); } } email.setFrom(from); email.setSubject(subject); email.setMsg(text); if (to != null) { for (String r : to) { email.addTo(r); } } if (cc != null) { for (String r : cc) { email.addCc(r); } } if (bcc != null) { for (String r : bcc) { email.addBcc(r); } } email.setSSLCheckServerIdentity(sslCheckServerIdentity); email.setStartTLSEnabled(tls); email.setStartTLSRequired(tls); return email; }
From source file:org.sonatype.nexus.internal.email.EmailManagerImpl.java
/** * Apply server configuration to email./*from w w w. j a va2 s . com*/ */ @VisibleForTesting Email apply(final EmailConfiguration configuration, final Email mail) throws EmailException { mail.setHostName(configuration.getHost()); mail.setSmtpPort(configuration.getPort()); mail.setAuthentication(configuration.getUsername(), configuration.getPassword()); mail.setStartTLSEnabled(configuration.isStartTlsEnabled()); mail.setStartTLSRequired(configuration.isStartTlsRequired()); mail.setSSLOnConnect(configuration.isSslOnConnectEnabled()); mail.setSSLCheckServerIdentity(configuration.isSslCheckServerIdentityEnabled()); mail.setSslSmtpPort(Integer.toString(configuration.getPort())); // default from address if (mail.getFromAddress() == null) { mail.setFrom(configuration.getFromAddress()); } // apply subject prefix if configured String subjectPrefix = configuration.getSubjectPrefix(); if (subjectPrefix != null) { String subject = mail.getSubject(); mail.setSubject(String.format("%s %s", subjectPrefix, subject)); } // do this last (mail properties are set up from the email fields when you get the mail session) if (configuration.isNexusTrustStoreEnabled()) { SSLContext context = trustStore.getSSLContext(); Session session = mail.getMailSession(); Properties properties = session.getProperties(); properties.remove(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_CLASS); properties.put(EmailConstants.MAIL_SMTP_SSL_ENABLE, true); properties.put("mail.smtp.ssl.socketFactory", context.getSocketFactory()); } return mail; }
From source file:sk.baka.webvm.analyzer.utils.NotificationDelivery.java
private static void configure(final Email mail, final Config config) throws EmailException { mail.setHostName(config.mailSmtpHost); config.mailSmtpEncryption.activate(mail); if (config.mailSmtpPort > 0) { if (mail.isSSL()) { mail.setSslSmtpPort(Integer.toString(config.mailSmtpPort)); } else {/* w ww . j ava 2 s .c om*/ mail.setSmtpPort(config.mailSmtpPort); } } if (config.mailSmtpUsername != null) { mail.setAuthentication(config.mailSmtpUsername, config.mailSmtpPassword); } mail.setFrom(config.mailFrom); if (config.mailTo == null) { config.mailTo = ""; } // add recipients final StringTokenizer t = new StringTokenizer(config.mailTo, ","); for (; t.hasMoreTokens();) { mail.addTo(t.nextToken().trim()); } }