SpringBoot邮箱发送和定时器

定时器:是springBoot自带的不用加依赖了

加上@EnableScheduleling注解在启动类

package com.nt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling//开启定时器的注解,springBoot自带了一个定时器
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

}


方式一:

package com.nt.util;


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

/*
* 定时任务类
*
* */

@Component
public class SchedulerTask {
    private int count=0;

    //每隔6秒打印一次
    @Scheduled(cron = "*/6 * * * * ?")
    public void process(){

        System.out.println("this is a scheduler task running" + (count++));


    }







}

方式二:


然后可以启动测试一下

邮箱:

  1. 在启动类步加上**@EableScheduling实现定时发送**
  2. 获取邮箱发送的授权码,登录qq邮箱,设置----》账户----》Pop3打开---->用手机发送短信可以开通,然后会出现相应的授权码,复制粘贴到下面的password中,
  3. 通过JavaMailSender这个接口可以实现邮箱的发送,通过SimpleMailMessage这个类可以设置相关的信息
  4. 可以用springbootTest进行测试
    发送邮箱需要配置application.yml文件:
server:
  port: 8888
spring:
  application:
    name: untitled

  mail:
    host: smtp.qq.com
    username:2282240015@qq.com
#    通过qq邮箱里面的设置 Stmp服务器的关闭,然后开启可以看到相碰的授权码
    password: ltvknhyomkmsecif
    default-encoding: utf-8
      #    添加上这三个配置,否则会报530错误
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

#  邮件的发送者
mail:
  fromMail:
    addr: 2282240015@qq.com

接口:

package com.nt.service;

public interface MailService {
//  to目的地   subject主题 cotent内容
    public void sendMail(String to,String subject,String cotent);
}

实现类:

package com.nt.service.impl;

import com.nt.service.MailService;
import com.sun.javaws.jnl.RContentDesc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;


@Service
public class MailServiceimpl implements MailService {
    @Autowired
   private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String from;

    @Override
    public void sendMail(String to, String subject, String cotent) {

        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(from);//发送者
        mailMessage.setTo(to);//接收者
        mailMessage.setSubject(subject);//设置主题
        mailMessage.setText(cotent);


        try {
            mailSender.send(mailMessage);
            System.out.println("发送邮件成功");

        }catch (Exception e){
            e.printStackTrace();
            System.out.println("邮箱发送失败");
        }













    }
}

也可以实现定时发送邮箱
util包下创建这个工具类:

package com.nt.util;

import com.nt.service.MailService;
import org.junit.jupiter.api.DynamicTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduleMail {
    @Autowired
    private MailService mailService;

    @Value("${mail.fromMail.addr}")

    private  String from;

    int count=0;

    @Scheduled(cron = "*/5 * * * * ?")
    public void SendMailtest(){
        mailService.sendMail(from,"simple mail","hahahhahahah"+(++count)+"邮箱");
        System.out.println("发送定时邮箱");
    }

}

启动类:

package com.nt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication

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

也可以用测试类实现:

package test;


import com.nt.MailApplication;
import com.nt.service.MailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MailApplication.class)
public class TestMail {
    @Autowired
    private MailService mailService;

    @Value("${mail.fromMail.addr}")

    private  String from;

    @Test
    public void SendMailtest(){
           mailService.sendMail(from,"simple mail","sucesss send mail");
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时间指导毕业设计

觉得写的好的话可以给我打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值