springboot简单使用定时(corn)和邮箱发送功能

本文介绍了如何在Spring Boot应用中配置JavaMail实现邮件发送,并展示了如何利用Spring Scheduling实现定时任务。内容包括配置`application.properties`、测试简单和复杂邮件、以及使用定时功能和异步延时功能的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在配置文件application.properties

spring.mail.username=7573548@qq.com
spring.mail.password=koapxsjyssdsdcbdjj(在自己邮箱设置拿到密码)
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

test类测试发送

package com.chen.springboot08assignment;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class Springboot08AssignmentApplicationTests {


    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {

        //一个简单的邮件
       SimpleMailMessage mailMessage= new SimpleMailMessage();
       mailMessage.setSubject("hello");
       mailMessage.setText("好好学习哈");
       mailMessage.setTo("1292331759046@qq.com");
       mailMessage.setFrom("7573548@qq.com");

        mailSender.send(mailMessage);
    }


    @Test
    void contextLoads2() throws MessagingException {

        //一个复杂的邮件
        MimeMessage mailMessage=mailSender.createMimeMessage();

        //组装
        MimeMessageHelper  helper=new MimeMessageHelper( mailMessage,true);
       //正文
        helper.setSubject("哈哈哈,下午好");
        helper.setText("<p style='color:red'>goodgoodstudy,daydayup</p>",true);

        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\86151\\Pictures\\Saved Pictures\\1.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\86151\\Pictures\\Saved Pictures\\1.jpg"));
        helper.setTo("1291759046@qq.com");
        helper.setFrom("7573548@qq.com");
        mailSender.send(mailMessage);
    }

}

使用定时功能

在springboot启动类添加注解

@EnableScheduling//开启定时功能的注解


@SpringBootApplication
public class Springboot08AssignmentApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot08AssignmentApplication.class, args);
    }

}

测试使用

package com.chen.springboot08assignment.service;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduledService {

    //在一个特定的时间执行这个方法

    //cron表达式
    //秒 分 时 日 月 周
    /*
    30 15 10 * * ?      代表每天10点15分执行一次
    30 0/5 10,18 * * ?   代表每天10点和18点,每隔五分钟执行一次
    0 15 10 ? * 1-6       每个月的周一到周六10.15分钟执行一次
    0/2 * * * * ?       每隔两秒执行一次
    */
    @Scheduled(cron="20 16 15 * * ?")
    public void hello(){
        System.out.println("hello,你被执行了~");
    }

}

使用异步延时功能

在springboot启动类添加注解


@EnableAsync//开启异步注解功能


@SpringBootApplication
public class Springboot08AssignmentApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot08AssignmentApplication.class, args);
    }

}

创建一个AsyncService类


@Service
public class AsyncService {

    //告诉spring这是一个异步的方法
    @Async
    public void hello(){
        try{

            Thread.sleep(3000);

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    System.out.println("数据正在处理。。。");

    }

}

创建一个控制类AsyncController


@RestController
public class AsyncController {


    @Autowired
    AsyncService asyncService;

    @RequestMapping("hello")
    public String hello(){
        asyncService.hello();//停止三秒,转圈
        return "ok";
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值