List of usage examples for org.apache.commons.mail Email setStartTLSRequired
public Email setStartTLSRequired(final boolean startTlsRequired)
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(//w w w. j a va 2s. co 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;/* w w w . java2 s . 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.openhab.binding.mail.internal.SMTPHandler.java
/** * use this server to send a mail/*w ww . j av a 2 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 w w 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 ava 2s. c o m */ @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:org.structr.common.MailHelper.java
private static void setup(final Email mail, final String to, final String toName, final String from, final String fromName, final String cc, final String bcc, final String bounce, final String subject) throws EmailException { // FIXME: this might be slow if the config file is read each time final Properties config = Services.getInstance().getCurrentConfig(); final String smtpHost = config.getProperty(Services.SMTP_HOST, "localhost"); final String smtpPort = config.getProperty(Services.SMTP_PORT, "25"); final String smtpUser = config.getProperty(Services.SMTP_USER); final String smtpPassword = config.getProperty(Services.SMTP_PASSWORD); final String smtpUseTLS = config.getProperty(Services.SMTP_USE_TLS, "true"); final String smtpRequireTLS = config.getProperty(Services.SMTP_REQUIRE_TLS, "false"); mail.setCharset(charset);/* w w w. j a va 2s . c om*/ mail.setHostName(smtpHost); mail.setSmtpPort(Integer.parseInt(smtpPort)); mail.setStartTLSEnabled(Boolean.parseBoolean(smtpUseTLS)); mail.setStartTLSRequired(Boolean.parseBoolean(smtpRequireTLS)); mail.setCharset(charset); mail.setHostName(smtpHost); mail.setSmtpPort(Integer.parseInt(smtpPort)); if (StringUtils.isNotBlank(smtpUser) && StringUtils.isNotBlank(smtpPassword)) { mail.setAuthentication(smtpUser, smtpPassword); } mail.addTo(to, toName); mail.setFrom(from, fromName); if (StringUtils.isNotBlank(cc)) { mail.addCc(cc); } if (StringUtils.isNotBlank(bcc)) { mail.addBcc(bcc); } if (StringUtils.isNotBlank(bounce)) { mail.setBounceAddress(bounce); } mail.setSubject(subject); }
From source file:paquetes.AlertSchedule.java
@Schedule(hour = "8", dayOfWeek = "*", info = "Todos los dias a las 8:00 a.m.") //@Schedule(second = "*", minute = "*/10", hour = "*", persistent= true, info = "cada 10 minutos") public void performTask() throws EmailException { long timeInit = System.currentTimeMillis(); ConfiguracionMail();//from www .j a va2 s.co m log.info(":. Inicio TareaProgramada cada dia"); try { timeInit = System.currentTimeMillis(); obtenerAlertas(); // Luego de obtener las alertas las recorremos para conformar cada uno de los selects de validacion alertas.stream().forEach((ale) -> { id_ale = ale.getId_ale(); llenarAlertasUsuarios(ale.getId_ale()); verificarPorTipoAlerta(ale); nom_tip_ale = ale.getNom_tip_ale(); logale.stream().forEach((lgal) -> { try { Email email = new SimpleEmail(); email.setHostName(this.hostname); email.setSmtpPort(Integer.parseInt(this.smtp_port)); email.setAuthenticator(new DefaultAuthenticator(this.user, this.pass)); // TLS agregado para server AWS Se quita comentario a setSSL. email.setStartTLSEnabled(true); email.setStartTLSRequired(true); // Comentariado para funcionar con Gmail //email.setSSLOnConnect(true); email.setFrom(this.remitente); email.setSubject(nom_tip_ale); email.setMsg(lgal.getAle_des()); alertasusuarios.stream().forEach((mailDestino) -> { try { email.addTo(mailDestino.getMailusu()); } catch (Exception e) { System.out.println("Error en la obtencion de destinatarios. " + e.getMessage()); } }); email.send(); } catch (Exception e) { System.out.println("Error en la conformacion del correo. " + e.getMessage()); } }); }); } catch (Exception e) { log.error("Error en la tarea programada"); log.info(e); } long time = System.currentTimeMillis(); time = System.currentTimeMillis() - timeInit; log.info(":. Fin tarea programada. Tiempo de proceso = " + time); }