SpringBoot_Mail_Test和部署
一、發(fā)送郵件
1、介紹
發(fā)送郵件應(yīng)該是網(wǎng)站的必備功能之一,什么注冊驗證,忘記密碼或者是給用戶發(fā)送營銷信息。最早期的時候我們會使用 JavaMail 相關(guān) api 來寫發(fā)送郵件的相關(guān)代碼,后來 Spring 推出了 JavaMailSender 更加簡化了郵件發(fā)送的過程,在之后 Spring Boot 對此進(jìn)行了封裝就有了現(xiàn)在的 spring-boot-starter-mail
2、引用依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
3、使用163郵箱發(fā)送配置
#郵箱服務(wù)器
spring.mail.host=smtp.163.com
#配置我的郵箱名
spring.mail.username=*********@163.com
#授權(quán)碼-登錄郵箱-設(shè)置-客戶端授權(quán)碼
spring.mail.password=***********
spring.mail.default-encoding=UTF-8
#打印日志信息
spring.mail.properties.mail.debug=true
4、發(fā)送郵件的Controller代碼
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MailController {
@Autowired
private JavaMailSender mail;
@Value("${spring.mail.username}")
private String form;
//純發(fā)送文本
@RequestMapping("/sendMail")
public Object sendMail1(String to, String title, String msg) {
SimpleMailMessage message = new SimpleMailMessage();
// 發(fā)件人郵箱、即便配置了也需要再次填寫
System.out.println("---------------" + form + "---------------");
message.setFrom(form);
// 收件人地址
message.setTo(to);
// 郵件標(biāo)題
message.setSubject(title);
// 郵件內(nèi)容
message.setText(msg);
try {
// 發(fā)送
mail.send(message);
return "ok";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
//發(fā)送html格式的郵件
@RequestMapping("/sendMail2")
public void sendHtmlMail2(String to, String subject, String content) throws Exception {
MimeMessage message = mail.createMimeMessage();
// true表示需要創(chuàng)建一個multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(form);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mail.send(message);
}
//發(fā)送帶附件的郵件
@RequestMapping("/sendMail3")
public void sendHtmlMail3(String to, String subject, String content, String filePath) throws Exception {
MimeMessage message = mail.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(form);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
//當(dāng)前項目的根目錄文件
FileSystemResource file = new FileSystemResource("src\\main\\resources\\"+filePath);
helper.addAttachment(filePath, file);
mail.send(message);
}
}
二、SpringBoot項目測試
1、SpringBoot默認(rèn)導(dǎo)入spring-boot-starter-testjar包、只需要額外導(dǎo)入導(dǎo)入JUnit5的jar包即可
2、編寫測試用例、可直接使用Spring管理的所有對象
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.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTest {
@Autowired
MailController info;
@Test
public void hello() {
System.out.println(info);
System.out.println("hello world");
}
}
3、測試Controller的所有方式
import org.junit.Before;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTest {
MockMvc mvc;
//初始化執(zhí)行
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new MailController()).build();
}
//驗證controller是否正常響應(yīng)并打印返回結(jié)果
@Test
public void getHello() throws Exception {
//發(fā)送請求、打印結(jié)果
mvc.perform(MockMvcRequestBuilders.get("/show1").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
//驗證controller是否正常響應(yīng)并判斷返回結(jié)果是否正確
@Test
public void testHello() throws Exception {
System.out.println("show222222222222222");
mvc.perform(MockMvcRequestBuilders.get("/show2?name=aaa").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print());
System.out.println("show222222222222222");
}
@Test
public void hello() {
System.out.println("hello world");
}
}
三、部署SpringBoot項目
nohup java -jar springboot001.jar &
# 后臺運行上述命令、
浙公網(wǎng)安備 33010602011771號