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); } }
