您好,登錄后才能下訂單哦!
這篇文章主要介紹Spring Boot如何利用Java Mail實現郵件發送,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
實現郵件發送的方法有很多,這里只是簡單記錄一個demo實現
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要配置成郵箱授權碼,不是郵箱密碼
簡單說下郵箱授權碼的獲取方式:
登錄網頁版QQ郵箱 > 設置 > 賬戶,需要保證 SMTP 服務開啟
<?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="你的郵箱授權碼(不是郵箱密碼)"/> <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配置信息
實現了一個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. 郵件發送類
簡單實現了一個郵件發送的類,其中包含發送郵件方法 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. 測試郵件發送
調用郵件發送,檢查是否發送成功:
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"; } }
附我的工程目錄,需要修改的就是圈出來的這幾個:
以上是“Spring Boot如何利用Java Mail實現郵件發送”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。