【发布时间】:2018-03-03 03:10:18
【问题描述】:
我尝试使用 SmtpAuthenticator 创建邮件服务。组件已正确启动,但用户名和密码字段中有空值。为什么会这样?
@Component
public class SmtpAuthenticator extends Authenticator {
private static final Logger LOG =
LogManager.getLogger(SmtpAuthenticator.class.getSimpleName());
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
public SmtpAuthenticator() {
LOG.info(SmtpAuthenticator.class.getSimpleName() + " started...");
LOG.debug("username=" + username);
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
LOG.debug("Username and password are correct...");
return new PasswordAuthentication(username, password);
}
LOG.error("Not correct mail login data!");
return null;
}
}
【问题讨论】:
-
您是否在
application属性/yaml 文件中定义了属性spring.mail.username和spring.mail.password?
标签: java spring spring-boot annotations