springboot实现定时发送邮件(可带附件)

首先在pom文件添加如下依赖

<properties>
        	<java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
<!--定时器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
<!--邮件-->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

在配置文件中进行如下配置

spring.application.name=spirng-boot-mail
#SMTP服务器地址,qq邮件是这个,别的邮箱需要自己查一下
spring.mail.host=smtp.qq.com
#发送邮件的邮箱
spring.mail.username=******@qq.com
#授权码
spring.mail.password=你自己的授权码,下面会告诉如何配置
spring.mail.default-encoding=UTF-8
#表明发送者
mail.fromMail.addr=******@qq.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

配置文件中需要填入授权码,下面就是如何获得授权码。首先,登录qq邮箱网页版,点击设置,点击账户
在这里插入图片描述下面这两个开启一个应该就可以,我开启的是IMAP/SMTP服务,也就是第二个。在这里插入图片描述开启之后,会让你用手机编辑短信,给指定号码发送一下短信,然后会生成授权码。这个授权码忘了也没关系,可以随时生成,只不过每次生成都需要发送一条短信。
这样,我们项目就配置好了,可以动手写代码了。
在启动类上加入@EnableScheduling注解来开启定时。
在这里插入图片描述
编写发送邮件的service层:

package com.example.service;

import javax.mail.MessagingException;

public interface MailService {
    
    void sendMail(String [] to,String subject,String content,String filename,String filepath) throws MessagingException;
}

在这里插入图片描述然后是impl层:



package com.example.service.impl;

import com.example.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

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

@Service
public class MailServiceImpl implements MailService{

    @Autowired
    JavaMailSender javaMailSender;//自动注入一个发送器
    @Override
    public void sendMail(String [] to,String subject,String content,String filename,String filepath) throws MessagingException {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject(subject);//标题
        helper.setText("<b><h1 style='color:red;'>"+content+"</h1></b>",true);//内容,可以用html设置样式,但是必须是true,默认是false
        helper.addAttachment(filename,new File(filepath));//附件,filename:在邮件中展示附件的名字,filepath在当前机器中的路径
        helper.setFrom("1304058312@qq.com");//发送人
        helper.setTo(to);
        javaMailSender.send(mimeMessage);

    }
}

在这里插入图片描述
最后编写定时任务的代码

package com.example.config;

import com.example.service.impl.MailServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;

@Component
public class Scheduler1Task {
    private Logger logger = LoggerFactory.getLogger(Scheduler1Task.class);
    private int count=0;
    @Autowired
    private MailServiceImpl mailService;

    //表示每隔56秒打印一次
    @Scheduled(cron = "*/56 * * * * ?")
    private void proces() throws MessagingException {

        String [] to = {"1304058312@qq.com","2910713446@qq.com","1378274944@qq.com"};
        logger.info("开始发送邮件,次数:"+(count++)+"次");
        mailService.sendMail(to,"猜猜我是谁","请按照照片猜猜我是谁!","中间的最帅.jpg","C:\\Users\\admin\\Desktop\\psb.jpg");
        logger.info("邮件发送成功!");
    }

}

最后附上一个在线cron表达式生成的网站,感觉还挺好用的:https://qqe2.com/cron

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值