當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
邮件发送---SpringBoot
生活随笔
收集整理的這篇文章主要介紹了
邮件发送---SpringBoot
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
郵件任務
郵件發送,在我們的日常開發中,也非常的多,Springboot也幫我們做了支持
- 郵件發送需要引入spring-boot-start-mail
- SpringBoot 自動配置MailSenderAutoConfiguration
- 定義MailProperties內容,配置在application.yml中
- 自動裝配JavaMailSender
- 測試郵件發送
測試:
1、引入pom依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId> </dependency>看它引入的依賴,可以看到 jakarta.mail
<dependency><groupId>com.sun.mail</groupId><artifactId>jakarta.mail</artifactId><version>1.6.4</version><scope>compile</scope> </dependency>2、查看自動配置類:MailSenderAutoConfiguration
這個類中存在bean,JavaMailSenderImpl
然后我們去看下配置文件
@ConfigurationProperties(prefix = "spring.mail" ) public class MailProperties {private static final Charset DEFAULT_CHARSET;private String host;private Integer port;private String username;private String password;private String protocol = "smtp";private Charset defaultEncoding;private Map<String, String> properties;private String jndiName; }3、配置文件:
spring.mail.username=24736743@qq.com spring.mail.password=你的qq授權碼 spring.mail.host=smtp.qq.com # qq需要配置ssl spring.mail.properties.mail.smtp.ssl.enable=true獲取授權碼:在QQ郵箱中的設置->賬戶->開啟pop3和smtp服務
4、Spring單元測試
@Autowired JavaMailSenderImpl mailSender;@Test public void contextLoads() {//郵件設置1:一個簡單的郵件SimpleMailMessage message = new SimpleMailMessage();message.setSubject("通知-明天來狂神這聽課");message.setText("今晚7:30開會");message.setTo("24736743@qq.com");message.setFrom("24736743@qq.com");mailSender.send(message); }@Test public void contextLoads2() throws MessagingException {//郵件設置2:一個復雜的郵件MimeMessage mimeMessage = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setSubject("通知-明天來狂神這聽課");helper.setText("<b style='color:red'>今天 7:30來開會</b>",true);//發送附件helper.addAttachment("1.jpg",new File(""));helper.addAttachment("2.jpg",new File(""));helper.setTo("24736743@qq.com");helper.setFrom("24736743@qq.com");mailSender.send(mimeMessage); }查看郵箱,郵件接收成功!
我們只需要使用Thymeleaf進行前后端結合即可開發自己網站郵件收發功能了!
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-L5BSFhog-1610187548376)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20210109181809891.png)]
總結
以上是生活随笔為你收集整理的邮件发送---SpringBoot的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单反相机报价(单反相机报价全幅)
- 下一篇: 莱卡数码相机(莱卡数码相机新款)