Skip to content

Instantly share code, notes, and snippets.

@xandreafonso
Created March 1, 2018 16:24
Show Gist options
  • Save xandreafonso/7d7e543710e2697ef7925b90944ea836 to your computer and use it in GitHub Desktop.
Save xandreafonso/7d7e543710e2697ef7925b90944ea836 to your computer and use it in GitHub Desktop.
Pegar propriedades de um arquivo externo com o Spring
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587
mail.smtp.username=seu-email-aqui@gmail.com
mail.smtp.password=suasenhaaqui
package com.algaworks.email.config;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
@PropertySource("file:/home/usuario/mail.properties") // ou, mais prático usar: "file:${user.home}/mail.properties"
public class MailConfig {
@Autowired
private Environment env;
@Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(env.getProperty("mail.smtp.host"));
mailSender.setPort(env.getProperty("mail.smtp.port", Integer.class));
mailSender.setUsername(env.getProperty("mail.smtp.username"));
mailSender.setPassword(env.getProperty("mail.smtp.password"));
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.connectiontimeout", 10000);
mailSender.setJavaMailProperties(props);
return mailSender;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment