创建一个邮箱
在设置里开启IMAP/SMTP服务(这里是126网易邮箱)
创建一个application.properties用来做配置文件
#编码
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.126.com
#密码
spring.mail.password=123456
#端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#邮箱号
spring.mail.username=tao_hg@126.com
创建一个java类用来向邮箱发送邮件
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MailController {
@Autowired
private JavaMailSender jms;//借助这个类,我们便可以发邮件了
@GetMapping("/send")
public String send(String message){
SimpleMailMessage ss=new SimpleMailMessage();
//发送邮箱
ss.setFrom("tao_hg@126.com");
//接收邮箱
ss.setTo("201883607@qq.com");
//标题
ss.setSubject("测试");
//内容
ss.setText("测试一下耍耍");
jms.send(ss);
return "1";
}
}
启动
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Test {
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}