如何使用Spring 4(和Spring Boot)通过基于纯注释的方法(根据Java配置规则)发送电子邮件?
一个简单的解决方案(您将使用未经身份验证的SMTP服务器)用于配置电子邮件服务,方法是:
@Configuration
public class MailConfig {
@Value("${email.host}")
private String host;
@Value("${email.port}")
private Integer port;
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(host);
javaMailSender.setPort(port);
javaMailSender.setJavaMailProperties(getMailProperties());
return javaMailSender;
}
private Properties getMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "false");
properties.setProperty("mail.smtp.starttls.enable", "false");
properties.setProperty("mail.debug", "false");
return properties;
}
}
春天必须能够解决性能email.host
和email.port
通常的方式(在春季启动的情况下,最简单的就是把那么application.properties)
在需要JavaMailSender服务的任何类中,只需使用一种常用方法(例如@Autowired private JavaMailSender javaMailSender
)注入
更新
请注意,自1.2.0.RC1版以来,Spring Boot可以JavaMailSender
为您自动配置。查看文档的这一部分。从文档中可以看到,几乎不需要任何配置即可启动和运行!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句