SpringBoot邮箱发送和定时器
Posted SmallCuteMonkey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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++));
}
}
方式二:
然后可以启动测试一下
邮箱:
- 在启动类步加上**@EableScheduling实现定时发送**
- 获取邮箱发送的授权码,登录qq邮箱,设置----》账户----》Pop3打开---->用手机发送短信可以开通,然后会出现相应的授权码,复制粘贴到下面的password中,
- 通过JavaMailSender这个接口可以实现邮箱的发送,通过SimpleMailMessage这个类可以设置相关的信息
- 可以用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");
}
}
以上是关于SpringBoot邮箱发送和定时器的主要内容,如果未能解决你的问题,请参考以下文章