Example usage for javax.mail PasswordAuthentication getUserName

List of usage examples for javax.mail PasswordAuthentication getUserName

Introduction

In this page you can find the example usage for javax.mail PasswordAuthentication getUserName.

Prototype

public String getUserName() 

Source Link

Usage

From source file:org.cloudcoder.healthmonitor.HealthMonitor.java

/**
 * Create a mail Session based on information in the
 * given {@link HealthMonitorConfig}.//from w w  w.  j a va 2  s  .  c  o  m
 * 
 * @param config the {@link HealthMonitorConfig}
 * @return the mail Session
 */
private Session createMailSession(HealthMonitorConfig config) {
    final PasswordAuthentication passwordAuthentication = new PasswordAuthentication(config.getSmtpUsername(),
            config.getSmtpPassword());

    Properties properties = new Properties();
    properties.putAll(System.getProperties());
    properties.setProperty("mail.smtp.submitter", passwordAuthentication.getUserName());
    properties.setProperty("mail.smtp.auth", "true");
    properties.setProperty("mail.smtp.host", config.getSmtpServer());
    properties.setProperty("mail.smtp.port", String.valueOf(config.getSmtpPort()));
    properties.setProperty("mail.smtp.starttls.enable", String.valueOf(config.isSmtpUseTLS()));

    return Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return passwordAuthentication;
        }
    });
}