Java Code Examples for org.springframework.mail.javamail.JavaMailSenderImpl#setProtocol()

The following examples show how to use org.springframework.mail.javamail.JavaMailSenderImpl#setProtocol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailService.java    From cloud-portal with MIT License 6 votes vote down vote up
@PostConstruct
public void init() {

	// create new java mail sender
	mailSender = new JavaMailSenderImpl();

	// set mail sender configuration
	mailSender.setHost(host);
	mailSender.setPort(port);
	mailSender.setProtocol(protocol);
	mailSender.setUsername(username);
	mailSender.setPassword(password);
	
	// set mail encoding
	mailSender.setDefaultEncoding(StandardCharsets.UTF_8.name());

	// create java mail properties
	Properties mailProperties = new Properties();
	mailProperties.put("mail.smtp.auth", auth);
	mailProperties.put("mail.smtp.starttls.enable", starttls);
	mailProperties.put("mail.smtp.timeout", timeout);    
	mailProperties.put("mail.smtp.connectiontimeout", timeout);   

	// set java mail properties
	mailSender.setJavaMailProperties(mailProperties);
}
 
Example 2
Source File: MailConfig.java    From restfiddle with Apache License 2.0 6 votes vote down vote up
@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;
   }
 
Example 3
Source File: SmtpMailer.java    From alf.io with GNU General Public License v3.0 6 votes vote down vote up
private static JavaMailSender toMailSender(Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> conf) {
    JavaMailSenderImpl r = new CustomJavaMailSenderImpl();
    r.setDefaultEncoding("UTF-8");

    r.setHost(conf.get(SMTP_HOST).getRequiredValue());
    r.setPort(Integer.parseInt(conf.get(SMTP_PORT).getRequiredValue()));
    r.setProtocol(conf.get(SMTP_PROTOCOL).getRequiredValue());
    r.setUsername(conf.get(SMTP_USERNAME).getValueOrDefault(null));
    r.setPassword(conf.get(SMTP_PASSWORD).getValueOrDefault(null));

    String properties = conf.get(SMTP_PROPERTIES).getValueOrDefault(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;
}
 
Example 4
Source File: JavaMailSenderFactory.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public JavaMailSenderImpl createMailSender(MailSettings mailSettings) {
  LOG.trace("createMailSender");
  JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
  mailSender.setHost(mailSettings.getHost());
  mailSender.setPort(mailSettings.getPort());
  mailSender.setProtocol(mailSettings.getProtocol());
  mailSender.setUsername(mailSettings.getUsername());
  mailSender.setPassword(mailSettings.getPassword());
  mailSender.setDefaultEncoding(mailSettings.getDefaultEncoding().name());
  Properties properties = new Properties(defaultProperties);
  defaultProperties.setProperty(MAIL_SMTP_STARTTLS_ENABLE, mailSettings.isStartTlsEnabled());
  defaultProperties.setProperty(MAIL_SMTP_QUITWAIT, mailSettings.isQuitWait());
  defaultProperties.setProperty(MAIL_SMTP_AUTH, mailSettings.isAuthenticationRequired());
  defaultProperties.setProperty(MAIL_SMTP_FROM_ADDRESS, mailSettings.getFromAddress());
  properties.putAll(mailSettings.getJavaMailProperties());
  LOG.debug("Mail properties: {}; defaults: {}", properties, defaultProperties);
  mailSender.setJavaMailProperties(properties);
  return mailSender;
}
 
Example 5
Source File: MailSenderFactory.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
private void setProperties(@NonNull JavaMailSenderImpl mailSender, @NonNull MailProperties mailProperties) {
    mailSender.setHost(mailProperties.getHost());
    mailSender.setPort(mailProperties.getPort());
    mailSender.setUsername(mailProperties.getUsername());
    mailSender.setPassword(mailProperties.getPassword());
    mailSender.setProtocol(mailProperties.getProtocol());
    mailSender.setDefaultEncoding(mailProperties.getDefaultEncoding().name());

    if (!CollectionUtils.isEmpty(mailProperties.getProperties())) {
        Properties properties = new Properties();
        properties.putAll(mailProperties.getProperties());
        mailSender.setJavaMailProperties(properties);
    }
}
 
Example 6
Source File: EmailConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSender mailSender() {
    final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(host);
    try {
        javaMailSender.setPort(Integer.valueOf(this.port));
    } catch (Exception e) {
    }
    javaMailSender.setUsername(username);
    javaMailSender.setPassword(password);
    javaMailSender.setProtocol(protocol);
    javaMailSender.setJavaMailProperties(loadProperties());
    return javaMailSender;
}
 
Example 7
Source File: EmailConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSender mailSender() {
    final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(host);
    try {
        javaMailSender.setPort(Integer.valueOf(this.port));
    } catch (Exception e) {
    }
    javaMailSender.setUsername(username);
    javaMailSender.setPassword(password);
    javaMailSender.setProtocol(protocol);
    javaMailSender.setJavaMailProperties(loadProperties());
    return javaMailSender;
}
 
Example 8
Source File: EmailConfiguration.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSenderImpl mailSender() {
    final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(host);
    if (StringUtils.isNumeric(port)) {
        javaMailSender.setPort(Integer.valueOf(this.port));
    }
    javaMailSender.setUsername(username);
    javaMailSender.setPassword(password);
    javaMailSender.setProtocol(protocol);
    javaMailSender.setJavaMailProperties(loadProperties());
    return javaMailSender;
}
 
Example 9
Source File: EmailConfiguration.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSenderImpl mailSender() {
    final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(host);
    if (StringUtils.isNumeric(port)) {
        javaMailSender.setPort(Integer.valueOf(this.port));
    }
    javaMailSender.setUsername(username);
    javaMailSender.setPassword(password);
    javaMailSender.setProtocol(protocol);
    javaMailSender.setJavaMailProperties(loadProperties());
    return javaMailSender;
}
 
Example 10
Source File: SendEmailService.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void fixAuth(JavaMailSenderImpl javaMailSender, Properties props) {
	if (javaMailSender.getPort() == 465) {
		props.put("mail.smtp.auth", true);
		props.put("mail.smtp.starttls.enable", false);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", false);
		props.put("mail.debug", true);
		javaMailSender.setProtocol("smtp");
	}
}
 
Example 11
Source File: SendEmailService.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void fixAuth(JavaMailSenderImpl javaMailSender, Properties props) {
	if (javaMailSender.getPort() == 465) {
		props.put("mail.smtp.auth", true);
		props.put("mail.smtp.starttls.enable", false);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", false);
		props.put("mail.debug", true);
		javaMailSender.setProtocol("smtp");
	}
}
 
Example 12
Source File: MailServiceImpl.java    From nimrod with MIT License 4 votes vote down vote up
@Override
public void initialize() {
    String host = (String) dictionaryService.get("MAIL", "HOST");
    String protocol = (String) dictionaryService.get("MAIL", "PROTOCOL");
    String port = (String) dictionaryService.get("MAIL", "PORT");
    String username = (String) dictionaryService.get("MAIL", "USERNAME");
    String password = (String) dictionaryService.get("MAIL", "PASSWORD");
    String defaultEncoding = (String) dictionaryService.get("MAIL", "DEFAULT_ENCODING");
    String smtpAuth = (String) dictionaryService.get("MAIL", "SMTP_AUTH");
    String startTlsEnable = (String) dictionaryService.get("MAIL", "STARTTLS_ENABLE");
    String startTlsRequired = (String) dictionaryService.get("MAIL", "STARTTLS_REQUIRED");

    javaMailSender = new JavaMailSenderImpl();
    if (host != null) {
        javaMailSender.setHost(host);
    }
    if (protocol != null) {
        javaMailSender.setProtocol(protocol);
    }
    if (port != null) {
        javaMailSender.setPort(Integer.parseInt(port));
    }
    if (username != null) {
        javaMailSender.setUsername(username);
    }
    if (password != null) {
        javaMailSender.setPassword(password);
    }
    if (password != null) {
        javaMailSender.setPassword(password);
    }
    if (defaultEncoding != null) {
        javaMailSender.setDefaultEncoding(defaultEncoding);
    }

    Properties properties = new Properties();
    if (smtpAuth != null) {
        properties.setProperty("mail.smtp.auth", smtpAuth);
    }
    if (startTlsEnable != null) {
        properties.setProperty("mail.starttls.enable", startTlsEnable);
    }
    if (startTlsRequired != null) {
        properties.setProperty("mail.starttls.required", startTlsRequired);
    }
    javaMailSender.setJavaMailProperties(properties);
}
 
Example 13
Source File: MailSenderFactoryBean.java    From rice with Educational Community License v2.0 4 votes vote down vote up
@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));
	    	}
	    }
	}
	
	// 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;
   }
 
Example 14
Source File: MailConfig.java    From QuizZz with MIT License 3 votes vote down vote up
@Bean
public JavaMailSender javaMailSender() {
	JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

	Properties mailProperties = new Properties();
	mailProperties.put("mail.smtp.auth", auth);
	mailProperties.put("mail.smtp.starttls.enable", starttls);
	mailSender.setJavaMailProperties(mailProperties);
	mailSender.setHost(host);
	mailSender.setPort(port);
	mailSender.setProtocol(protocol);
	mailSender.setUsername(username);
	mailSender.setPassword(password);

	return mailSender;
}