# 定时
## 在application类添加定时注解
```java
// 定时任务 注解
@EnableScheduling
```
## 在定时方法上添加注解
```java
// cron表达式: 秒 分 时 天 月 年
// 特殊字符: /每隔 ,枚举 L最后 W工作日 -范围
// 例:cron = "1/6 0-30 14,15,16 l * *" -> 每年每月的最后一天14 15 16点 前30分钟 从第1秒开始每隔6秒 触发一次
@Scheduled(cron = " 0/5 * * * * *")
public void scheduled()
{
System.out.println(System.currentTimeMillis());
}
```
# 发送邮件
## 导包
```html
spring-boot-starter-mail
```
## 配置
```properties
#邮箱账号
spring.mail.username=yudr_cq@hqyj.com
#授权码
spring.mail.password=weqUD27nUbSxYR4E
#SMTP服务器
spring.mail.host=smtp.qiye.163.com
#SMTP服务器端口
spring.mail.port=465
#发送邮件协议
spring.mail.protocol=smtp
spring.mail.default-encoding=utf-8
#启用SSL安全通道
spring.mail.properties.mail.smtp.ssl.enable=true
#启用授权
spring.mail.properties.mail.smtp.auth=true
#发件邮箱账号
spring.mail.username=1486058673@QQ.COM
#授权码
spring.mail.password=uwjtfpkrjukrhfef
#SMTP服务器
spring.mail.host=smtp.qq.com
#SMTP服务器端口
spring.mail.port=465
#发送邮件协议
spring.mail.protocol=smtp
spring.mail.default-encoding=utf-8
#启用SSL安全通道
spring.mail.properties.mail.smtp.ssl.enable=true
#启用授权
spring.mail.properties.mail.smtp.auth=true
```
## 发件
```java
package com.lbb.vue01_exercise_service.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin
@RequestMapping
@RestController
public class Mail
{
// 引入邮件发送类
@Autowired
private MailSender mailSender;
@RequestMapping("/sendMail")
public String sendMail()
{
SimpleMailMessage message = new SimpleMailMessage();
// 设置发件人邮箱地址,跟配置中spring.mail.username一致
message.setFrom("1486058673@QQ.COM");
// 设置收件人地址
message.setTo("1185545917@QQ.COM");
// 设置邮件标题
message.setSubject("xxx");
// 设置邮件内容
message.setText("xxx");
//发送邮件
mailSender.send(message);
return "发送成功";
}
}
```



