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

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

Introduction

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

Prototype

public synchronized void setSession(Session session) 

Source Link

Document

Set the JavaMail Session , possibly pulled from JNDI.

Usage

From source file:fr.mycellar.configuration.MailConfiguration.java

@Bean
public JavaMailSender javaMailSender() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setSession(session);
    return javaMailSender;
}

From source file:org.kuali.rice.core.mail.MailSenderFactoryBean.java

@Override
protected Object createInstance() throws Exception {
    // Retrieve "mail.*" properties from the configuration system and construct a Properties object
    Properties properties = new Properties();
    Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
    LOG.debug("createInstance(): collecting mail properties.");
    for (Object keyObj : configProps.keySet()) {
        if (keyObj instanceof String) {
            String key = (String) keyObj;
            if (key.startsWith(MAIL_PREFIX)) {
                properties.put(key, configProps.get(key));
            }//from  w ww  .  ja v  a2 s.co m
        }
    }

    // Construct an appropriate Java Mail Session
    // If username and password properties are found, construct a Session with SMTP authentication
    String username = properties.getProperty(USERNAME_PROPERTY);
    String password = properties.getProperty(PASSWORD_PROPERTY);
    if (username != null && password != null) {
        mailSession = Session.getInstance(properties, new SimpleAuthenticator(username, password));
        LOG.info("createInstance(): Initializing mail session using SMTP authentication.");
    } else {
        mailSession = Session.getInstance(properties);
        LOG.info("createInstance(): Initializing mail session. No SMTP authentication.");
    }

    // Construct and return a Spring Java Mail Sender
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    LOG.debug("createInstance(): setting SMTP host.");
    mailSender.setHost(properties.getProperty(HOST_PROPERTY));
    if (properties.getProperty(PORT_PROPERTY) != null) {
        LOG.debug("createInstance(): setting SMTP port.");
        int smtpPort = Integer.parseInt(properties.getProperty(PORT_PROPERTY).trim());
        mailSender.setPort(smtpPort);
    }
    String protocol = properties.getProperty(PROTOCOL_PROPERTY);
    if (StringUtils.isNotBlank(protocol)) {
        LOG.debug("createInstance(): setting mail transport protocol = " + protocol);
        mailSender.setProtocol(protocol);
    }
    mailSender.setSession(mailSession);

    LOG.info("createInstance(): Mail Sender Factory Bean initialized.");
    return mailSender;
}