日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

SpringBoot中整合Mail实现发送邮件

發(fā)布時(shí)間:2025/3/19 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot中整合Mail实现发送邮件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

場(chǎng)景

項(xiàng)目搭建專欄:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/35688

實(shí)現(xiàn)最簡(jiǎn)單的帶標(biāo)題以及文本內(nèi)容的郵件發(fā)送。使用qq郵件服務(wù)器。

實(shí)現(xiàn)

項(xiàng)目中引入郵件依賴

<!-- 郵件依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>

找到項(xiàng)目的全局配置文件application.properties

將username改為要發(fā)送郵件的賬號(hào),這里是qq郵箱賬號(hào)

下面password設(shè)置為qq郵箱的授權(quán)碼

#JavaMail郵件發(fā)送的配置 #指明郵件發(fā)送服務(wù)器? 如果是163的則為smtp.163.com spring.mail.host=smtp.qq.com spring.mail.username= #授權(quán)碼qq郵箱需要設(shè)置并獲取授權(quán)碼? 163則直接為郵箱密碼 spring.mail.password= spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true

qq郵箱設(shè)置授權(quán)碼:

qq郵箱--設(shè)置--賬戶--POP3/SMTP服務(wù)

開(kāi)啟服務(wù)后就會(huì)獲得qq的授權(quán)碼

在項(xiàng)目下新建email包

在email包下新建配置類實(shí)現(xiàn)從全局配置文件中獲取自定義屬性

package com.example.demo.email;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class EmailConfig {@Value("${spring.mail.username}")private String emailFrom;public String getEmailFrom() {return emailFrom;}public void setEmailFrom(String emailFrom) {this.emailFrom = emailFrom;}}

新建接口EmailService,有一個(gè)發(fā)送郵件的方法。

package com.example.demo.email;import org.springframework.stereotype.Service;@Service public interface EmailService {void sendSimpleMail(String senfTo,String title,String content); }

新建接口實(shí)現(xiàn)類

package com.example.demo.email;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailServiceImpl implements EmailService {@Autowiredprivate EmailConfig emailConfig;@Autowiredprivate JavaMailSender mailSender;@Overridepublic void sendSimpleMail(String sendTo, String title, String content) {//簡(jiǎn)單郵件的發(fā)送SimpleMailMessage message = new SimpleMailMessage();message.setFrom(emailConfig.getEmailFrom());message.setTo(sendTo);message.setSubject(title);message.setText(content);mailSender.send(message);}}

通過(guò)SimpleMailMessage 設(shè)置要發(fā)送郵件的賬號(hào)以及標(biāo)題和內(nèi)容。

然后使用JavaMailSender 實(shí)現(xiàn)發(fā)送郵件。

新建controller實(shí)現(xiàn)測(cè)試郵件發(fā)送。

package com.example.demo.email;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class EmailController {@Autowiredprivate EmailService emailService;@RequestMapping("simpleEmail")@ResponseBodypublic String sendSimpleEmail() {emailService.sendSimpleMail("****@qq.com", "測(cè)試", "簡(jiǎn)單郵件");return "success";} }

sendSimpleMail方法第一個(gè)參數(shù)就是要發(fā)送的郵箱賬號(hào)。

項(xiàng)目啟動(dòng)類下添加此包為可掃描。

@SpringBootApplication(scanBasePackages= {"com.example.demo.controller","com.example.demo.service","com.example.demo.interceptor","com.example.demo.handler","com.example.demo.job","com.example.demo.email"})

啟動(dòng)項(xiàng)目,訪問(wèn)

http://localhost:8080/simpleEmail

可以看到瀏覽器返回success,然后收到郵件。

效果

?

?

源碼下載

https://download.csdn.net/download/badao_liumang_qizhi/11114781

總結(jié)

以上是生活随笔為你收集整理的SpringBoot中整合Mail实现发送邮件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。