华清远见 Java学习-定时

# 定时

## 在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

<!--发送邮件-->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-mail</artifactId>

</dependency>

```

## 配置

```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 "发送成功";

    }

}

```


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值