引入依赖
org.springframework.boot spring-boot-starter-mail
配置application.properties
#邮箱名称 spring.mail.username=1258073422@qq.com #邮箱授权码 https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001256&&id=28 spring.mail.password=binjdydururkbaei spring.mail.host=smtp.qq.com # qq需要开启加密验证 spring.mail.properties.smtp.ssl.enable=true
测试:
@SpringBootTest
class DemoApplicationTests {
@Autowired
JavaMailSenderImpl javaMailSender;
@Test
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("发一个邮件");
message.setText("发邮箱");
message.setFrom("1258073422@qq.com");
message.setTo("1258073422@qq.com");
javaMailSender.send(message);
}
}
这样就收到了
在启动类上加上 @EnableScheduling//开启定时任务 注解
测试一下
@Service
public class ScheduledService {
//定时 cron表达式
@Scheduled(cron = "01 09 20 * * ?")
public void hello(){
System.out.println(System.currentTimeMillis()+"执行了");
}
}
启动之后 等待一会



