日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

1 分钟教会你用 Spring Boot 发邮件

發布時間:2025/3/21 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1 分钟教会你用 Spring Boot 发邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring Boot 提供了一個發送郵件的簡單抽象,使用的是下面這個接口。

org.springframework.mail.javamail.JavaMailSender

Spring Boot 提供了一個?starter,并能自動配置,下面來做個小例子,順便解析它做了什么工作。

?

0、你所需具備的基礎

  • 什么是 Spring Boot?

  • Spring Boot 核心配置文件詳解

  • Spring Boot 開啟的 2 種方式

  • Spring Boot 自動配置原理、實戰

  • Spring Boot 2.x 啟動全過程源碼分析

?

1、添加依賴

在 Maven?pom.xml?配置文件中加入?spring-boot-starter-mail?依賴。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId> </dependency>

?

2、添加配置參數

然后在?application.properties?文件中加入以下配置。

spring.mail.host=smtp.exmail.qq.com spring.mail.username=admin@javastack.cn spring.mail.password=123456#?啟動SSL時的配置 spring.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory spring.mail.smtp.socketFactory.fallback=false spring.mail.smtp.socketFactory.port=465

?

3、一個簡單的發送郵件例子

寫一個控制器,寫一個簡單的發送郵件的小例子,發送成功返回?true,發送失敗返回?false。

@Autowired private?JavaMailSender?javaMailSender;@RequestMapping("/sendEmail") @ResponseBody public?boolean?sendEmail()?{SimpleMailMessage?msg?=?new?SimpleMailMessage();msg.setFrom("admin@javastack.cn");msg.setBcc();msg.setTo("admin@javastack.cn");msg.setSubject("Java技術棧投稿");msg.setText("技術分享");try?{javaMailSender.send(msg);}?catch?(MailException?ex)?{System.err.println(ex.getMessage());return?false;}return?true; }

?

4、自動配置都做了什么?

Spring Boot 發現類路徑下有這個?spring-boot-starter-mail?包和?spring.mail.host?參數就會自動配置?JavaMailSenderImpl。

上面那些?spring.mail.xx?參數用來裝配?MailProperties?這個類。

org.springframework.boot.autoconfigure.mail.MailProperties

自動配置類:

org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

@Configuration @ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class }) @ConditionalOnMissingBean(MailSender.class) @Conditional(MailSenderCondition.class) @EnableConfigurationProperties(MailProperties.class) @Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class }) public class MailSenderAutoConfiguration {/*** Condition to trigger the creation of a {@link MailSender}. This kicks in if either* the host or jndi name property is set.*/static class MailSenderCondition extends AnyNestedCondition {MailSenderCondition() {super(ConfigurationPhase.PARSE_CONFIGURATION);}@ConditionalOnProperty(prefix = "spring.mail", name = "host")static class HostProperty {}@ConditionalOnProperty(prefix = "spring.mail", name = "jndi-name")static class JndiNameProperty {}}}

?

org.springframework.boot.autoconfigure.mail.MailSenderPropertiesConfiguration

@Configuration @ConditionalOnProperty(prefix = "spring.mail", name = "host") class MailSenderPropertiesConfiguration {private final MailProperties properties;MailSenderPropertiesConfiguration(MailProperties properties) {this.properties = properties;}@Bean@ConditionalOnMissingBeanpublic JavaMailSenderImpl mailSender() {JavaMailSenderImpl sender = new JavaMailSenderImpl();applyProperties(sender);return sender;}private void applyProperties(JavaMailSenderImpl sender) {sender.setHost(this.properties.getHost());if (this.properties.getPort() != null) {sender.setPort(this.properties.getPort());}sender.setUsername(this.properties.getUsername());sender.setPassword(this.properties.getPassword());sender.setProtocol(this.properties.getProtocol());if (this.properties.getDefaultEncoding() != null) {sender.setDefaultEncoding(this.properties.getDefaultEncoding().name());}if (!this.properties.getProperties().isEmpty()) {sender.setJavaMailProperties(asProperties(this.properties.getProperties()));}}private Properties asProperties(Map<String, String> source) {Properties properties = new Properties();properties.putAll(source);return properties;}}

?

其實就是用了上面裝配的參數注冊了一個?JavaMailSenderImpl?實例而已,然后你就可以注入使用了。

總結

以上是生活随笔為你收集整理的1 分钟教会你用 Spring Boot 发邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。