SpringBoot简单使用邮件只需三步

环境:SpringBoot2.02

在SpringBoot中使用email服务很简单了,之前毕设项目中使用的是javax.mail的jar

现在重做发现Springboot早已经有了封装好的email依赖

第一步:pom中添加依赖

	    
			org.springframework.boot
			spring-boot-starter-mail
		

第二步:配置文件中application.properties配置邮件设置

## 163邮件服务
spring.mail.host=smtp.163.com
[email protected]
## 注意password是授权码,需到163邮箱中点设置,设置授权码,并开启SMTP等服务
spring.mail.password=kk140680909
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.enable=true
spring.mail.properties.mail.smtp.required=true

第三步:Controller中写个方法进行测试


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class EmailTest {
	
	@Autowired
	JavaMailSender javaMailSender;
	@RequestMapping(value = "/mailTest",method = RequestMethod.GET)
	public void mailTest() {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom("[email protected]");
		message.setTo("[email protected]");
		message.setSubject("主题:简单邮件");
	    message.setText("测试邮件内容");
	    javaMailSender.send(message);
	}
}

然后。然后就没有然后了

你可能感兴趣的:(SpringBoot,SpringBoot使用说明书)