Skill/spring

SMTPConfig.java

진열사랑 2025. 1. 13. 10:37

@Data
@Configuration
@ConfigurationProperties(prefix="smtp")
public class SMTPConfig {
private String host;
private String port;
private Boolean auth;
private Boolean startTLSEnable;
private String user;
private String password;

/**--------------------------------------------------------------------
* ■서비스 등록
--------------------------------------------------------------------**/
@Bean
public JavaMailSender getMailSender() {
JavaMailSenderImpl objJavaMailSenderImpl = new JavaMailSenderImpl();

objJavaMailSenderImpl.setJavaMailProperties(getMailProperties());

// 인증 계정이 있는 경우
if(auth) {
objJavaMailSenderImpl.setUsername(user);
objJavaMailSenderImpl.setPassword(password);

Session session = Session.getInstance(getMailProperties(), new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
session.setDebug(true);
}

return objJavaMailSenderImpl;
}

/**--------------------------------------------------------------------
* ■SMTP 프로퍼티 설정
--------------------------------------------------------------------**/
private Properties getMailProperties() {
Properties properties = new Properties();

properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.starttls.enable", startTLSEnable ? "true" : "false");
properties.setProperty("mail.smtp.ssl.trust", host);
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.auth", auth ? "true" : "false");
properties.setProperty("mail.smtp.port", port);
properties.setProperty("mail.smtp.socketFactory.port", port);
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

return properties;
}
}