Springboot定时发送邮件,并附带Excel文件和PDF文件
1、导入jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
注意: 先进入QQ邮箱,点击设置,再点击帐户
在帐户中找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,开启IMAP/SMTP服务,获取密码
2、配置application.yml
spring:
mail:
#邮箱服务器地址
host: smtp.qq.com #QQ邮箱
username: xxxxxxxxx@qq.com #QQ邮箱
password: xxxxxxxxxx #开启邮箱服务的密码
default-encoding: UTF-8
3、我是把发送、接收和抄送的邮箱都配置在了application.yml,如果你不想配置在yml文件里可以跳过这一步
mail:
fromMail:
addr: xxxxxxx@qq.com #发送邮件的邮箱
toMail:
to: xxxxxxx@qq.com #接收邮件的邮箱
ccone: xxxxxxx@hotmail.com #第一个抄送邮箱
cctwo: xxxxxxxx@gmail.com #第二个抄送邮箱
bcc: xxxxxxx@qq.com #隐秘抄送邮箱
4、创建发送邮件方法
4.1、MailService
/** * @program: * @description: 定时发送邮件Service * @author: SaffiChan * @create: 2021-04-13 09:16 **/ public interface MailService { void sendHtmlMail(String subject, String content, String filePathExcel, String filePathPdf); }
4.2、MailServiceImpl
import com.well.supermarket.service.MailService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.regex.Pattern; /** * @program: * @description: 定时发送邮件ServiceImpl * @author: SaffiChan * @create: 2021-04-13 09:18 **/ @Component @Service public class MailServiceImpl implements MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private JavaMailSender mailSender; @Value("${mail.fromMail.addr}") private String from; @Value("${mail.toMail.to}") private String to; @Value("${mail.toMail.ccone}") private String ccone; @Value("${mail.toMail.cctwo}") private String cctwo; @Value("${mail.toMail.bcc}") private String bcc; /** * 发送html邮件 * * @param * @param subject * @param content */ @Override public void sendHtmlMail(String subject, String content, String filePathExcel, String filePathPdf) { MimeMessage message = mailSender.createMimeMessage(); try { //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); //配置在yml文件中的发件人邮箱 helper.setTo(to); //配置在yml文件中的收件人邮箱 String[] cc = {ccone,cctwo}; //将两个抄送邮箱放进String[]中作为一个参数传值 helper.setCc(cc); //设置抄送人邮箱 helper.setBcc(bcc); //设置隐秘抄送人邮箱 helper.setSubject(subject); //设置标题 helper.setText(content, true); //设置邮件内容,并开启H5 // 判断是否带有Excel附件 if (filePathExcel != null) { FileSystemResource file = new FileSystemResource(new File(filePathExcel)); String fileName = filePathExcel.substring(filePathExcel.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); } // 判断是否带有Pdf附件 if (filePathPdf != null) { FileSystemResource file = new FileSystemResource(new File(filePathPdf)); String fileName = filePathPdf.substring(filePathPdf.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); } mailSender.send(message); logger.info("定时报表邮件发送成功"); } catch (MessagingException e) { logger.error("发送定时报表邮件时发生异常!", e); } } }
5、创建定时任务
import com.well.supermarket.service.MailService;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;import java.util.Date;/** * @program: * @description: 发送邮件定时任务 * @author: SaffiChan * @create: 2021-04-13 09:23 **/ @Component public class EmailSchedulerTask { @Autowired private MailService mailService; /** * 每天凌晨00:02分执行,corn表达式可根据自己需求定义 */ @Scheduled(cron="0 02 00 * * ? ") private void processDay() throws Exception { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String emailContent = formatter.format(new Date())+" 日報表"; mailService.sendHtmlMail(formatter.format(new Date())+"日報表",emailContent, filePathExcel,filePathPdf); } }