您好,登錄后才能下訂單哦!
前言:以前都是直接用Java自帶的郵件工具發送郵件,現在Spring幫我們做了封裝,提供了更好用更簡單的發送郵件工具JavaMailSender,關于郵件服務器的設置就不在這里說了,直接去QQ郵箱設置下就好,下面看下主要的步驟:
步驟一、添加發送郵件相關Maven依賴
<!-- Spring 郵件發送 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
步驟二、添加相關Spring配置,創建一個spring-mail.xml(如果是Spring-Boot請無視),內容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- Spring JavaMailSender --> <bean class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.qq.com"></property> <!-- 或許你可以用465端口,默認的25不適合 --> <property name="port" value="587"/> <property name="protocol" value="smtp"/> <property name="username" value="******@qq.com"></property> <!-- 這里的是你在QQ郵箱發送開通SMTP短信后,獲取的授權碼 --> <property name="password" value="******"></property> <property name="defaultEncoding" value="UTF-8"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.timeout">25000</prop> </props> </property> </bean> </beans>
步驟三、創建具體的郵件發送工具類
/** * 基于Spring的JavaMailSender的郵件發送工具類 * @author simon * */ @Component public class EmailSender { @Autowired private JavaMailSender javaMailSender; private static EmailSender emailSender; @PostConstruct public void init() { emailSender = this; emailSender.javaMailSender= this.javaMailSender; } /** * 發送簡單的文本郵件 * @param sendTo 收件人組 * @param subject 主題 * @param content 文本內容 */ public static void sendSimpleMessage(String sendFrom, String[] sendTo, String subject, String textcontent) { SimpleMailMessage mail = new SimpleMailMessage(); mail.setFrom(sendFrom); mail.setTo(sendTo); mail.setSubject(subject); mail.setText(textcontent); //發送 emailSender.javaMailSender.send(mail); } /** * 發送HTML內容格式的郵件 * @param sendFrom * @param sendTo 收件人組 * @param subject 主題 * @param htmlContent HTML內容 * @throws Exception */ public static void sendHtmlMessage(String sendFrom, String[] sendTo, String subject, String htmlContent) throws Exception { MimeMessage mimeMessage = emailSender.javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); mimeMessageHelper.setFrom(sendFrom); mimeMessageHelper.setTo(sendTo); mimeMessageHelper.setSubject(subject); // true 表示啟動HTML格式的郵件 mimeMessageHelper.setText(htmlContent, true); // 發送郵件 emailSender.javaMailSender.send(mimeMessage); } }
步驟四、創建單元測試測試發送郵件
/** * 郵件發送測試類 * * @author simon * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class EmailSenderTest { @Test public void testSend() throws Exception { String sendFrom = "1317492210@qq.com"; String[] sendTo = {"zhaosheng@hitencent.com"}; String subject = "Spring自帶JavaMailSender發送的HTML郵件"; StringBuilder htmlContent = new StringBuilder() .append("<html>") .append("<head>") .append("<title>") .append("Spring自帶JavaMailSender發送的HTML郵件") .append("</title>") .append("</head>") .append("<body>") .append("您好!陌生人<p/>") .append("</body>") .append("</html>"); EmailSender.sendHtmlMessage(sendFrom, sendTo, subject, htmlContent.toString()); System.out.println("郵件發送成功."); }
OK,到這里全部的步驟就結束了,是不是比Java Mail簡單方便很多哉,趕緊自己動手試試吧!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。