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

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

Introduction

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

Prototype

public void setJavaMailProperties(Properties javaMailProperties) 

Source Link

Document

Set JavaMail properties for the Session .

Usage

From source file:com.restfiddle.config.mail.MailConfig.java

@Bean
public JavaMailSender javaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    Properties mailProperties = new Properties();

    mailSender.setProtocol(env.getProperty("mail.protocol"));
    mailSender.setHost(env.getProperty("mail.host"));
    mailSender.setPort(Integer.parseInt(env.getProperty("mail.port")));

    mailSender.setUsername(env.getProperty("mail.username"));
    mailSender.setPassword(env.getProperty("mail.password"));

    mailProperties.put("mail.smtp.auth", env.getProperty("mail.smtp.auth"));
    mailProperties.put("mail.smtp.starttls.enable", env.getProperty("mail.smtp.starttls.enable"));
    mailProperties.put("mail.smtp.debug", env.getProperty("mail.smtp.debug"));

    mailProperties.put("mail.smtp.socketFactory.port", "465");
    mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    mailProperties.put("mail.smtps.ssl.trust", env.getProperty("mail.smtps.ssl.trust"));
    mailProperties.put("mail.smtps.ssl.checkserveridentity",
            env.getProperty("mail.smtps.ssl.checkserveridentity"));

    mailSender.setJavaMailProperties(mailProperties);

    return mailSender;
}

From source file:org.trustedanalytics.user.invite.config.InvitationsConfig.java

@Bean(name = "emailService")
protected EmailService emailService() throws UnsupportedEncodingException {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();

    int port = smtpProperties.getPort();

    if (port > 0) {
        sender.setPort(port);/* www .j  a  v a 2s. c om*/
    }
    sender.setProtocol(smtpProperties.getProtocol());
    sender.setHost(smtpProperties.getHost());

    Properties mailProps = new Properties();

    if (!StringUtils.isBlank(smtpProperties.getUsername())
            && !StringUtils.isBlank(smtpProperties.getPassword())) {
        sender.setUsername(smtpProperties.getUsername());
        sender.setPassword(smtpProperties.getPassword());
        mailProps.setProperty(String.format("mail.%s.auth", smtpProperties.getProtocol()), "true");
    } else {
        mailProps.setProperty(String.format("mail.%s.auth", smtpProperties.getProtocol()), "false");
    }

    if ("smtps".equals(smtpProperties.getProtocol())) {
        mailProps.setProperty("mail.smtps.ssl.enable", "true");
    }

    mailProps.setProperty("mail.smtps.connectiontimeout", Integer.toString(smtpProperties.getTimeout()));

    if (smtpProperties.isDebug()) {
        mailProps.setProperty("mail.debug", "true");
        System.setProperty("mail.socket.debug", "true");
    }

    sender.setJavaMailProperties(mailProps);

    return new EmailService(sender, smtpProperties.getEmail(), smtpProperties.getEmailName());
}

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.
 * /* www  .ja  v  a2 s .c  o  m*/
 * @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:alfio.manager.system.SmtpMailer.java

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

    r.setHost(configurationManager//  www  .j  ava 2  s .c  o  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:nl.strohalm.cyclos.entities.settings.MailSettings.java

public JavaMailSender getMailSender() {
    if (mailSender == null) {
        final JavaMailSenderImpl impl = new JavaMailSenderImpl();
        impl.setHost(smtpServer);//from   ww w  . j av  a 2 s . c  o  m
        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;
}

From source file:org.apache.syncope.core.provisioning.java.job.notification.NotificationJobDelegate.java

@Override
public void afterPropertiesSet() throws Exception {
    if (mailSender instanceof JavaMailSenderImpl) {
        JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;

        Properties javaMailProperties = javaMailSender.getJavaMailProperties();

        Properties props = PropertyUtils.read(Encryptor.class, "mail.properties", "conf.directory").getLeft();
        for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
            String prop = (String) e.nextElement();
            if (prop.startsWith("mail.smtp.")) {
                javaMailProperties.setProperty(prop, props.getProperty(prop));
            }//from  w  ww . ja  va2 s.  c o  m
        }

        if (StringUtils.isNotBlank(javaMailSender.getUsername())) {
            javaMailProperties.setProperty("mail.smtp.auth", "true");
        }

        javaMailSender.setJavaMailProperties(javaMailProperties);

        String mailDebug = props.getProperty("mail.debug", "false");
        if (BooleanUtils.toBoolean(mailDebug)) {
            Session session = javaMailSender.getSession();
            session.setDebug(true);
            session.setDebugOut(new PrintStream(new LogOutputStream(LOG)));
        }
    }
}

From source file:org.emonocot.service.impl.JavaMailSenderImplFactory.java

/**
 * @return the instantiate JavaMailSender
 *///  ww  w .j  a va2  s.co  m
private void instantiate() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    if (StringUtils.isNotBlank(username)) {
        javaMailSender.setUsername(username);
    }
    if (StringUtils.isNotBlank(password)) {
        javaMailSender.setPassword(password);
    }
    javaMailSender.setJavaMailProperties(javaMailProperties);
    sender = javaMailSender;
}

From source file:org.haiku.haikudepotserver.config.AppConfig.java

@Bean
public MailSender mailSender(@Value("${smtp.host}") String host, @Value("${smtp.port:25}") Integer port,
        @Value("${smtp.auth:false}") Boolean smtpAuth, @Value("${smtp.starttls:false}") Boolean startTls,
        @Value("${smtp.username:}") String username, @Value("${smtp.password:}") String password) {

    Properties properties = new Properties();
    properties.put("mail.smtp.auth", smtpAuth);
    properties.put("mail.smtp.starttls.enable", startTls);

    JavaMailSenderImpl result = new JavaMailSenderImpl();

    result.setHost(host);/*from  www . ja  v  a 2s. com*/
    result.setPort(port);
    result.setProtocol("smtp");
    result.setJavaMailProperties(properties);

    if (StringUtils.isNotBlank(username)) {
        result.setUsername(username);
    }

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

    return result;
}

From source file:org.kuali.kra.service.impl.KcEmailServiceImpl.java

private JavaMailSender createSender() {
    Properties props = getConfigProperties();
    JavaMailSenderImpl sender = null;

    if (props != null && StringUtils.isNotBlank(props.getProperty("mail.smtp.host"))
            && StringUtils.isNotBlank(props.getProperty("mail.smtp.port"))
            && StringUtils.isNotBlank(props.getProperty("mail.smtp.user"))
            && StringUtils.isNotBlank(props.getProperty("mail.user.credentials"))) {
        try {/*from  w  w  w.j a v  a 2  s.c o  m*/
            sender = new JavaMailSenderImpl();

            sender.setJavaMailProperties(props);
            sender.setHost(props.getProperty("mail.smtp.host"));
            sender.setPort(Integer.parseInt(props.getProperty("mail.smtp.port")));
            sender.setUsername(props.getProperty("mail.smtp.user"));
            sender.setPassword(props.getProperty("mail.user.credentials"));
        } catch (Exception e) {
            LOG.warn(
                    "Unable to create email sender due to invalid configuration. The properties [mail.smtp.host, "
                            + "mail.smtp.port, mail.smtp.user, mail.user.credentials] need to be set.");
            sender = null;
        }
    } else {
        LOG.warn("Unable to create email sender due to missing configuration.  The properties [mail.smtp.host, "
                + "mail.smtp.port, mail.smtp.user, mail.user.credentials] need to be set.");
    }

    return sender;
}

From source file:org.mifosplatform.infrastructure.reportmailingjob.service.ReportMailingJobEmailServiceImpl.java

@Override
public void sendEmailWithAttachment(ReportMailingJobEmailData reportMailingJobEmailData) {
    try {/* ww w . jav a 2s. c o  m*/
        // get all ReportMailingJobConfiguration objects from the database
        this.reportMailingJobConfigurationDataCollection = this.reportMailingJobConfigurationReadPlatformService
                .retrieveAllReportMailingJobConfigurations();

        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setHost(this.getReportSmtpServer());
        javaMailSenderImpl.setPort(this.getRerportSmtpPort());
        javaMailSenderImpl.setUsername(this.getReportSmtpUsername());
        javaMailSenderImpl.setPassword(this.getReportSmtpPassword());
        javaMailSenderImpl.setJavaMailProperties(this.getJavaMailProperties());

        MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();

        // use the true flag to indicate you need a multipart message
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

        mimeMessageHelper.setTo(reportMailingJobEmailData.getTo());
        mimeMessageHelper.setFrom(this.getReportSmtpFromAddress());
        mimeMessageHelper.setText(reportMailingJobEmailData.getText());
        mimeMessageHelper.setSubject(reportMailingJobEmailData.getSubject());

        if (reportMailingJobEmailData.getAttachment() != null) {
            mimeMessageHelper.addAttachment(reportMailingJobEmailData.getAttachment().getName(),
                    reportMailingJobEmailData.getAttachment());
        }

        javaMailSenderImpl.send(mimeMessage);
    }

    catch (MessagingException e) {
        // handle the exception
        e.printStackTrace();
    }
}