重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
本篇文章給大家分享的是有關(guān)如何使用Spring Boot實(shí)現(xiàn)一個(gè)郵件發(fā)送功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)扎囊免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
1、引入依賴
org.springframework.boot spring-boot-starter-mail
2、參數(shù)配置
在application.properties中配置郵件相關(guān)的參數(shù)
spring.thymeleaf.cache=false spring.mail.host=smtp.qq.com spring.mail.username=***@qq.com spring.mail.password=ymwrdffauajebgde //此處的密碼時(shí)qq郵箱的授權(quán)碼 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.stattls.required=true
3、郵件Service代碼
@Service public class MailService { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender sender; /*發(fā)送郵件的方法*/ public void sendSimple(String to, String title, String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); //發(fā)送者 message.setTo(to); //接受者 message.setSubject(title); //發(fā)送標(biāo)題 message.setText(content); //發(fā)送內(nèi)容 sender.send(message); System.out.println("郵件發(fā)送成功"); } }
4、編寫頁面代碼
Insert title here 郵件發(fā)送
5、郵件請(qǐng)求處理
@Controller public class MailController { @Autowired private MailService mailService; private String to="***@qq.com"; @RequestMapping("mail") public String mail(){ return "/mail"; } @RequestMapping("sendMail") @ResponseBody public String sendMail(@RequestParam("title")String title){ System.out.println("-----title: " + title); mailService.sendSimple(to, title, title); return "success"; } }
6、測(cè)試
7、qq郵箱授權(quán)碼
以上就是如何使用Spring Boot實(shí)現(xiàn)一個(gè)郵件發(fā)送功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。