Example usage for org.springframework.mail.javamail JavaMailSenderImpl setPassword

List of usage examples for org.springframework.mail.javamail JavaMailSenderImpl setPassword

Introduction

In this page you can find the example usage for org.springframework.mail.javamail JavaMailSenderImpl setPassword.

Prototype

public void setPassword(@Nullable String password) 

Source Link

Document

Set the password for the account at the mail host, if any.

Usage

From source file:de.iteratec.iteraplan.businesslogic.service.notifications.JavaMailSenderFactory.java

/**
 * Creates the instance of {@link JavaMailSenderImpl}. The returned sender is configured and
 * ready to use. If the notifications are not activated, {@code null} will be returned.
 * /*ww w.  j  a  v  a  2s.  com*/
 * @return configured instance of {@link JavaMailSenderImpl} or {@code null} if notifications are not activated
 */
public JavaMailSender create() {
    if (!activated) {
        return null;
    }

    if (StringUtils.isBlank(getHost())) {
        throw new IteraplanTechnicalException(IteraplanErrorMessages.NOTIFICATION_CONFIGURATION_INCOMPLETE);
    }

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Configuring mail sender framework with hostname " + getHost() + ", port " + getPort()
                + ". SSL connections enabled: " + isSsl() + ", STARTTLS enabled: " + isStartTls()
                + ", username " + getUsername() + ", password " + (getPassword() != null ? "***" : "null"));
    }
    sender.setHost(getHost());
    sender.setUsername(getUsername());
    sender.setPassword(getPassword());

    if (StringUtils.isNotBlank(getPort())) {
        sender.setPort(Integer.parseInt(getPort()));
    }

    Properties properties = new Properties();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(
                "Turned on mail sender framework's DEBUG logging. It will be written to the console, respectively catalina.out");
        properties.put("mail.debug", "true");
    }

    if (isStartTls()) {
        properties.put("mail.smtp.starttls.enable", "true");
    }

    if (isSsl()) {
        properties.put("mail.smtp.ssl.enable", "true");
    }
    sender.setJavaMailProperties(properties);

    return sender;
}

From source file:org.brushingbits.jnap.email.EmailSender.java

public void send(Email email) {
    EmailAccountInfo accountInfo = defaultEmailAccount;
    JavaMailSenderImpl sender = this.defaultMailSender;
    if (email.getAccountInfo() != null) {
        accountInfo = email.getAccountInfo();
        synchronized (this.mailSenderMap) {
            sender = this.mailSenderMap.get(accountInfo);
            if (sender == null) {
                sender = new JavaMailSenderImpl();
                Properties props = new Properties(this.defaultEmailAccount.getJavaMailProperties());
                props.putAll(accountInfo.getJavaMailProperties());
                sender.setJavaMailProperties(props);
                sender.setUsername(accountInfo.getUsername());
                sender.setPassword(accountInfo.getPassword());
                this.mailSenderMap.put(accountInfo, sender);
            }/*from   www  .  j ava2 s  .co  m*/
        }
    }
    sender.send((MimeMessagePreparator) email);
}

From source file:org.jnap.core.email.EmailSender.java

public void send(Email email) {
    EmailAccountInfo accountInfo = defaultEmailAccount;
    JavaMailSenderImpl sender = this.defaultMailSender;
    if (email.getAccountInfo() != null) {
        accountInfo = email.getAccountInfo();
        synchronized (this.mailSenderMap) {
            sender = this.mailSenderMap.get(accountInfo);
            if (sender == null) {
                sender = new JavaMailSenderImpl();
                Properties props = new Properties(this.defaultEmailAccount.getJavaMailProperties());
                props.putAll(accountInfo.getJavaMailProperties());
                sender.setJavaMailProperties(props);
                sender.setUsername(accountInfo.getUsername());
                sender.setPassword(accountInfo.getPassword());
                this.mailSenderMap.put(accountInfo, sender);
            }/*from  w  w w.  j  a  v a 2 s .c  o m*/
        }
    } else {
        email.setAccountInfo(accountInfo);
    }
    sender.send((MimeMessagePreparator) email);
}

From source file:nz.net.orcon.kanban.automation.actions.EmailSenderAction.java

public void sendSecureEmail(String subject, String emailBody, String to, String bcc, String from,
        String replyTo, String host, String password) {

    SimpleMailMessage mailMessage = new SimpleMailMessage();
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

    mailSender.setHost(host);//from  www .j av a 2 s . c  o  m
    mailSender.setPort(587);
    mailSender.setProtocol("smtp");

    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");

    mailSender.setJavaMailProperties(props);

    if (StringUtils.isNotBlank(to)) {
        mailMessage.setTo(to);
    }
    if (StringUtils.isNotBlank(bcc)) {
        mailMessage.setBcc(bcc);
    }

    if (StringUtils.isNotBlank(from)) {
        mailMessage.setFrom(from);
        mailSender.setUsername(from);
    }

    if (StringUtils.isNotBlank(password)) {
        mailSender.setPassword(password);
    }

    if (StringUtils.isNotBlank(replyTo)) {
        mailMessage.setReplyTo(replyTo);
    }

    if (StringUtils.isNotBlank(subject)) {
        mailMessage.setSubject(subject);
    }

    mailMessage.setText(emailBody);
    mailSender.send(mailMessage);
    logger.info("Secure Email Message has been sent..");
}

From source file:nu.yona.server.CoreConfiguration.java

@Bean
public JavaMailSender javaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", yonaProperties.getEmail().getSmtp().isEnableAuth());
    mailProperties.put("mail.smtp.starttls.enable", yonaProperties.getEmail().getSmtp().isEnableStartTls());
    mailSender.setJavaMailProperties(mailProperties);
    mailSender.setHost(yonaProperties.getEmail().getSmtp().getHost());
    mailSender.setPort(yonaProperties.getEmail().getSmtp().getPort());
    mailSender.setProtocol(yonaProperties.getEmail().getSmtp().getProtocol());
    mailSender.setUsername(yonaProperties.getEmail().getSmtp().getUsername());
    mailSender.setPassword(yonaProperties.getEmail().getSmtp().getPassword());
    return mailSender;
}

From source file:alfio.manager.system.SmtpMailer.java

private JavaMailSender toMailSender(Event event) {
    JavaMailSenderImpl r = new CustomJavaMailSenderImpl();
    r.setDefaultEncoding("UTF-8");

    r.setHost(configurationManager//www  .j  a v  a 2s  .co m
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_HOST)));
    r.setPort(Integer.valueOf(configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PORT))));
    r.setProtocol(configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROTOCOL)));
    r.setUsername(configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_USERNAME), null));
    r.setPassword(configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PASSWORD), null));

    String properties = configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROPERTIES), null);

    if (properties != null) {
        try {
            Properties prop = PropertiesLoaderUtils.loadProperties(new EncodedResource(
                    new ByteArrayResource(properties.getBytes(StandardCharsets.UTF_8)), "UTF-8"));
            r.setJavaMailProperties(prop);
        } catch (IOException e) {
            log.warn("error while setting the mail sender properties", e);
        }
    }
    return r;
}

From source file:com.thinkbiganalytics.metadata.sla.TestConfiguration.java

@Bean(name = "slaEmailSender")
public JavaMailSender javaMailSender(
        @Qualifier("slaEmailConfiguration") EmailConfiguration emailConfiguration) {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", emailConfiguration.getSmtpAuth());
    mailProperties.put("mail.smtp.starttls.enable", emailConfiguration.getStarttls());
    if (StringUtils.isNotBlank(emailConfiguration.getSmptAuthNtmlDomain())) {
        mailProperties.put("mail.smtp.auth.ntlm.domain", emailConfiguration.getSmptAuthNtmlDomain());
    }/*ww w .  j a va2 s  .  c o  m*/
    mailProperties.put("mail.debug", "true");

    mailSender.setJavaMailProperties(mailProperties);
    mailSender.setHost(emailConfiguration.getHost());
    mailSender.setPort(emailConfiguration.getPort());
    mailSender.setProtocol(emailConfiguration.getProtocol());
    mailSender.setUsername(emailConfiguration.getUsername());
    mailSender.setPassword(emailConfiguration.getPassword());
    return mailSender;
}

From source file:com.thinkbiganalytics.metadata.sla.config.EmailServiceLevelAgreementSpringConfiguration.java

@Bean(name = "slaEmailSender")
public JavaMailSender javaMailSender(
        @Qualifier("slaEmailConfiguration") EmailConfiguration emailConfiguration) {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

    Properties mailProperties = mailSender.getJavaMailProperties();
    mailProperties.put("mail.smtp.auth", StringUtils.defaultIfBlank(emailConfiguration.getSmtpAuth(), "false"));
    mailProperties.put("mail.smtp.starttls.enable",
            StringUtils.defaultIfBlank(emailConfiguration.getStarttls(), "false"));

    if (StringUtils.isNotBlank(emailConfiguration.getStarttlsRequired())) {
        mailProperties.put("mail.smtp.starttls.required", emailConfiguration.getStarttlsRequired());
    }/*from w  w  w  .j  a v  a 2 s  .  co  m*/
    if (StringUtils.isNotBlank(emailConfiguration.getSmptAuthNtmlDomain())) {
        mailProperties.put("mail.smtp.auth.ntlm.domain", emailConfiguration.getSmptAuthNtmlDomain());
    }

    mailProperties.put("mail.smtp.connectiontimeout",
            StringUtils.defaultIfBlank(emailConfiguration.getSmtpConnectionTimeout(), "5000"));
    mailProperties.put("mail.smtp.timeout",
            StringUtils.defaultIfBlank(emailConfiguration.getSmtpTimeout(), "5000"));
    mailProperties.put("mail.smtp.writetimeout",
            StringUtils.defaultIfBlank(emailConfiguration.getSmtpWriteTimeout(), "5000"));

    if (StringUtils.isNotBlank(emailConfiguration.getSslEnable())) {
        mailProperties.put("mail.smtp.ssl.enable", emailConfiguration.getSslEnable());
    }
    if (StringUtils.isNotBlank(emailConfiguration.getDebug())) {
        mailProperties.put("mail.debug", emailConfiguration.getDebug());
    }

    mailSender.setHost(emailConfiguration.getHost());
    mailSender.setPort(emailConfiguration.getPort());
    mailSender.setProtocol(emailConfiguration.getProtocol());
    mailSender.setUsername(emailConfiguration.getUsername());
    mailSender.setPassword(emailConfiguration.getPassword());

    return mailSender;
}

From source file:com.glaf.mail.business.MailSendThread.java

public void run() {
    logger.debug("---------------send mail----------------------------");
    if (mailItem != null && MailTools.isMailAddress(mailItem.getMailTo())) {
        /**//  w w w .jav a 2 s. c  o  m
         * ?5????
         */
        if (mailItem.getRetryTimes() > 5) {
            return;
        }
        /**
         * ????????
         */
        if (mailItem.getSendStatus() == 1) {
            return;
        }
        /**
         * ????
         */
        if (mailItem.getLastModified() < (System.currentTimeMillis() - DateUtils.DAY * 30)) {
            return;
        }

        MailAccount mailAccount = null;
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        List<MailAccount> accounts = task.getAccounts();
        if (accounts.size() > 1) {
            java.util.Random r = new java.util.Random();
            mailAccount = accounts.get(Math.abs(r.nextInt(accounts.size())));
        } else {
            mailAccount = accounts.get(0);
        }

        logger.debug("send account:" + mailAccount);

        String content = task.getContent();

        if (StringUtils.isEmpty(task.getCallbackUrl())) {
            String serviceUrl = SystemConfig.getServiceUrl();
            String callbackUrl = serviceUrl + "/website/mail/receive/view";
            task.setCallbackUrl(callbackUrl);
        }

        if (StringUtils.isNotEmpty(task.getCallbackUrl())) {
            MxMailHelper mailHelper = new MxMailHelper();
            String messageId = "{taskId:\"" + task.getId() + "\",itemId:\"" + mailItem.getId() + "\"}";
            messageId = RequestUtils.encodeString(messageId);
            String url = task.getCallbackUrl() + "?messageId=" + messageId;
            content = mailHelper.embedCallbackScript(content, url);
        }

        MailMessage mailMessage = new MailMessage();
        mailMessage.setFrom(mailAccount.getMailAddress());
        mailMessage.setTo(mailItem.getMailTo());
        mailMessage.setContent(content);
        mailMessage.setSubject(task.getSubject());
        mailMessage.setSaveMessage(false);

        javaMailSender.setHost(mailAccount.getSmtpServer());
        javaMailSender.setPort(mailAccount.getSendPort() > 0 ? mailAccount.getSendPort() : 25);
        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
        javaMailSender.setUsername(mailAccount.getUsername());
        if (StringUtils.isNotEmpty(mailAccount.getPassword())) {
            String pwd = RequestUtils.decodeString(mailAccount.getPassword());
            javaMailSender.setPassword(pwd);
            // logger.debug("send account pwd:"+pwd);
        }
        javaMailSender.setDefaultEncoding("GBK");

        try {
            mailItem.setSendDate(new Date());
            mailItem.setRetryTimes(mailItem.getRetryTimes() + 1);
            MailSender mailSender = ContextFactory.getBean("mailSender");
            mailSender.send(javaMailSender, mailMessage);
            mailItem.setSendStatus(1);
        } catch (Throwable ex) {
            mailItem.setSendStatus(-1);
            if (LogUtils.isDebug()) {
                logger.debug(ex);
                ex.printStackTrace();
            }
            if (ex instanceof javax.mail.internet.ParseException) {
                mailItem.setSendStatus(-10);
            } else if (ex instanceof javax.mail.AuthenticationFailedException) {
                mailItem.setSendStatus(-20);
            } else if (ex instanceof javax.mail.internet.AddressException) {
                mailItem.setSendStatus(-30);
            } else if (ex instanceof javax.mail.SendFailedException) {
                mailItem.setSendStatus(-40);
            } else if (ex instanceof java.net.UnknownHostException) {
                mailItem.setSendStatus(-50);
            } else if (ex instanceof java.net.SocketException) {
                mailItem.setSendStatus(-60);
            } else if (ex instanceof java.io.IOException) {
                mailItem.setSendStatus(-70);
            } else if (ex instanceof java.net.ConnectException) {
                mailItem.setSendStatus(-80);
            } else if (ex instanceof javax.mail.MessagingException) {
                mailItem.setSendStatus(-90);
                if (ex.getMessage().indexOf("response: 554") != -1) {
                    mailItem.setSendStatus(-99);
                }
            }
        } finally {
            mailDataFacede.updateMail(task.getId(), mailItem);
        }
    }
}

From source file:nl.strohalm.cyclos.entities.settings.MailSettings.java

public JavaMailSender getMailSender() {
    if (mailSender == null) {
        final JavaMailSenderImpl impl = new JavaMailSenderImpl();
        impl.setHost(smtpServer);/*from w ww.  j a va2  s .c  om*/
        impl.setPort(smtpPort);
        final Properties properties = new Properties();
        if (StringUtils.isNotEmpty(smtpUsername)) {
            // Use authentication
            properties.setProperty("mail.smtp.auth", "true");
            impl.setUsername(smtpUsername);
            impl.setPassword(smtpPassword);
        }
        if (smtpUseTLS) {
            properties.setProperty("mail.smtp.starttls.enable", "true");
        }
        impl.setJavaMailProperties(properties);
        mailSender = impl;
    }
    return mailSender;
}