使用Java批注通过Spring发送电子邮件

使用Java批注通过Spring发送电子邮件,第1张

使用Java批注通过Spring发送电子邮件

一个简单的解决方案(您将使用没有身份验证的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;    }}

spring必须能够解决性能

email.host
email.port
通常的方式(在spring启动的情况下,最简单的就是把那么application.properties)

在需要JavaMailSender服务的任何类中,只需使用一种常用方法(例如

@Autowired private JavaMailSenderjavaMailSender
)注入


更新

请注意,自1.2.0.RC1版开始,Spring
Boot可以

JavaMailSender
为您自动配置。查看文档的这一部分。从文档中可以看到,几乎不需要任何配置即可启动和运行!



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5641511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存