這篇文章主要介紹Spring Boot如何利用Java Mail實(shí)現(xiàn)郵件發(fā)送,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
目前成都創(chuàng)新互聯(lián)已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、民權(quán)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
實(shí)現(xiàn)郵件發(fā)送的方法有很多,這里只是簡單記錄一個(gè)demo實(shí)現(xiàn)
1. 引入maven依賴
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </dependency>
2. 配置郵箱bean信息
我的配置文件名稱是application-bean.xml
我是用qq郵箱做的測試,這里需要注意的是password要配置成郵箱授權(quán)碼,不是郵箱密碼
簡單說下郵箱授權(quán)碼的獲取方式:
登錄網(wǎng)頁版QQ郵箱 > 設(shè)置 > 賬戶,需要保證 SMTP 服務(wù)開啟
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.qq.com"/> <property name="port" value="465"/> <property name="defaultEncoding" value="utf-8"/> <property name="username" value="你的郵箱地址"/> <property name="password" value="你的郵箱授權(quán)碼(不是郵箱密碼)"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.sendpartial">true</prop> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.socketFactory.port">465</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> </props> </property> </bean> <bean class="com.amwalle.walle.util.Mail"> <property name="mailSender" ref="mailSender"/> </bean> </beans>
3. 加載bean配置信息
實(shí)現(xiàn)了一個(gè)BeanConfig類:
package com.amwalle.walle.bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource(locations={"classpath:/application-bean.xml"}) public class BeanConfig { }
4. 郵件發(fā)送類
簡單實(shí)現(xiàn)了一個(gè)郵件發(fā)送的類,其中包含發(fā)送郵件方法 sendMail:
package com.amwalle.walle.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.internet.MimeMessage; public class Mail { private static final Logger logger = LoggerFactory.getLogger(Mail.class); private static JavaMailSenderImpl mailSender; public static void sendMail(String title, String html, String to) { try { MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true); helper.setFrom(mailSender.getUsername()); helper.setTo(to); helper.setSubject(title); helper.setText(html, true); mailSender.send(msg); } catch (Exception e) { logger.error("sendMail error", e); } } public void setMailSender(JavaMailSenderImpl mailSender) { Mail.mailSender = mailSender; } public JavaMailSenderImpl getMailSender() { return mailSender; } }
5. 測試郵件發(fā)送
調(diào)用郵件發(fā)送,檢查是否發(fā)送成功:
package com.amwalle.walle.controller; import com.amwalle.walle.util.Mail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class HelloWorldController { private static final Logger logger = LoggerFactory.getLogger(HelloWorldController.class); @RequestMapping(value = "/walle", method = RequestMethod.GET) public String helloWorld() { logger.info("--------test-------"); Mail.sendMail("test","test", "接收郵箱地址"); return "hello world"; } }
附我的工程目錄,需要修改的就是圈出來的這幾個(gè):
以上是“Spring Boot如何利用Java Mail實(shí)現(xiàn)郵件發(fā)送”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:SpringBoot如何利用JavaMail實(shí)現(xiàn)郵件發(fā)送
本文路徑:http://m.newbst.com/article48/gpgjhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、用戶體驗(yàn)、Google、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)