專案中用到Spring Boot發QQ郵箱給女朋友
摘要:
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
[email protected]
spring.mail.default-encoding=UTF-8
spring.mail.port=4...
spring.mail.host=smtp.qq.com spring.mail.protocol=smtp [email protected] spring.mail.default-encoding=UTF-8 spring.mail.port=465 spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.debug=true spring.mail.password=hpqxqvacriwibdbe 複製程式碼
7.單元測試
@RunWith(SpringRunner.class) @SpringBootTest public class MailApplicationTests { @Autowired JavaMailSender mailSender; @Test public void contextLoads() { SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo("[email protected]"); msg.setCc("[email protected]"); msg.setSubject("這是一封測試情書"); msg.setFrom("[email protected]"); msg.setSentDate(new Date()); msg.setText("哈哈!你行不行啊"); mailSender.send(msg); } 複製程式碼
8.用網頁格式傳送需要導pom.xml依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 複製程式碼
9.寫個main.html頁
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p>歡迎<span style="color: #ffff11;" th:text="${username}"></span> 加入 XXX 大家庭</p> <div>你的入職資訊如下:</div> <table torder="1"> <tr> <td>職位</td> <td>th:text="${position}"</td> </tr> <tr> <td>薪資</td> <td th:text="${salary}"></td> </tr> </table> </body> </html> 複製程式碼
10.單元測試
package org.javaboy.mail; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMailMessage; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import javax.mail.Message; import javax.mail.MessageRemovedException; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.util.Date; @RunWith(SpringRunner.class) @SpringBootTest public class MailApplicationTests { @Autowired JavaMailSender mailSender; @Autowired TemplateEngine templateEngine; @Test public void contextLoads() { SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo("[email protected]"); msg.setSubject("這是一封情書"); msg.setFrom("[email protected]"); msg.setSentDate(new Date()); msg.setText("約會嗎"); mailSender.send(msg); } @Test public void test1() throws MessagingException{ MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage); helper.setCc("[email protected]"); helper.setCc("[email protected]"); helper.setSubject("給我打萬塊錢"); helper.setFrom("[email protected]"); helper.setSentDate(new Date()); Context context = new Context(); context.setVariable("username","libobo"); context.setVariable("position","老婆"); context.setVariable("salary","10000"); String mail = templateEngine.process("main", context); helper.setText(mail,true); mailSender.send(mimeMessage); } } 複製程式碼