小鸡炸

导航

SpringBoot_Mail_Test和部署

一、发送邮件

1、介绍

发送邮件应该是网站的必备功能之一,什么注册验证,忘记密码或者是给用户发送营销信息。最早期的时候我们会使用 JavaMail 相关 api 来写发送邮件的相关代码,后来 Spring 推出了 JavaMailSender 更加简化了邮件发送的过程,在之后 Spring Boot 对此进行了封装就有了现在的 spring-boot-starter-mail

2、引用依赖

<dependencies>
	<dependency> 
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-mail</artifactId>
	</dependency> 
</dependencies>

3、使用163邮箱发送配置

#邮箱服务器
spring.mail.host=smtp.163.com
#配置我的邮箱名
spring.mail.username=*********@163.com
#授权码-登录邮箱-设置-客户端授权码
spring.mail.password=***********
spring.mail.default-encoding=UTF-8
#打印日志信息
spring.mail.properties.mail.debug=true

4、发送邮件的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;

	//纯发送文本
	@RequestMapping("/sendMail")
	public Object sendMail1(String to, String title, String msg) {
		SimpleMailMessage message = new SimpleMailMessage();
		// 发件人邮箱、即便配置了也需要再次填写
		System.out.println("---------------" + form + "---------------");
		message.setFrom(form);
		// 收件人地址
		message.setTo(to);
		// 邮件标题
		message.setSubject(title);
		// 邮件内容
		message.setText(msg);
		try {
			// 发送
			mail.send(message);
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	//发送html格式的邮件
	@RequestMapping("/sendMail2")
	public void sendHtmlMail2(String to, String subject, String content) throws Exception {
		MimeMessage message = mail.createMimeMessage();
		// true表示需要创建一个multipart message
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(form);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);

		mail.send(message);
	}

	//发送带附件的邮件
	@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);

		//当前项目的根目录文件
		FileSystemResource file = new FileSystemResource("src\\main\\resources\\"+filePath);
		helper.addAttachment(filePath, file);
		mail.send(message);
	}
}

二、SpringBoot项目测试

1、SpringBoot默认导入spring-boot-starter-testjar包、只需要额外导入导入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;

    //初始化执行
    @Before
    public void setUp() throws Exception {
    	mvc = MockMvcBuilders.standaloneSetup(new MailController()).build();
    }

    //验证controller是否正常响应并打印返回结果
    @Test
    public void getHello() throws Exception {
    	//发送请求、打印结果
        mvc.perform(MockMvcRequestBuilders.get("/show1").accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        
    }
    
    //验证controller是否正常响应并判断返回结果是否正确
    @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 &
# 后台运行上述命令、

posted on 2022-01-19 10:51  小鸡炸  阅读(52)  评论(0编辑  收藏  举报