基于SpringBoot實(shí)現(xiàn)定時(shí)發(fā)送郵件過(guò)程解析
前提:
1.Springboot項(xiàng)目
2.引入maven 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
以下代碼中涉及到的maven依賴有日志依賴,但是springboot都有集成,不用重新引入依賴

Application(程序入口)
package com.springbootemaildemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 引入了一個(gè)注解@EnableSwagger2來(lái)啟動(dòng)swagger注解。(啟動(dòng)該注解使得用在controller中的swagger注解生效, 覆蓋的范圍由@ComponentScan的配置來(lái)指定,
* 這里默認(rèn)指定為根路徑”com.springboot”下的所有controller)
* 也可以單獨(dú)寫(xiě)衣swaggerConfigura
*/
@EnableScheduling //啟動(dòng)定時(shí)任務(wù)
@EnableSwagger2 //啟動(dòng)swagger注解
@SpringBootApplication
public class MailApplication {
public static void main(String[] args) {
SpringApplication.run(MailApplication.class, args);
}
}
MailJob(定時(shí)任務(wù)類)
package com.springbootemaildemo.job;
import com.springbootemaildemo.send.SendMail;
import com.springbootemaildemo.send.TenSenvenMail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@EnableScheduling
public class MailJob {
private static final Logger logger = LoggerFactory.getLogger(MailJob.class);
@Resource
SendMail sendMail;
@Resource
TenSenvenMail tenSenvenMail;
//@Scheduled(cron = "0/5 * * * * ?")
//或直接指定時(shí)間間隔,例如:100秒
// @Scheduled(fixedRate=100000)
//早晨7點(diǎn)
@Scheduled(cron = "0 0 7 * * ?")
public void sendJob() {
String bodyTen = "早安哇,太陽(yáng)出來(lái)啦,記得開(kāi)心喲";
String bodyWen = "記得開(kāi)心喲";
logger.info("定時(shí)任務(wù)開(kāi)始..........................");
sendMail.sendWen(bodyWen);
tenSenvenMail.sendTen(bodyTen);
logger.info("定時(shí)任務(wù)結(jié)束..........................");
}
}
@EnableScheduling 這個(gè)注解是 開(kāi)啟定時(shí)任務(wù)。
發(fā)送郵件代碼:
發(fā)送普通的郵件(發(fā)送郵件類):
package com.springbootemaildemo.send;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
@Component
public class SendMail {
private static final Logger logger = LoggerFactory.getLogger(SendMail.class);
public void sendWen(String body) {
logger.info("開(kāi)始發(fā)送..................");
String from = "212212@qq.com";
String to = "5456456@qq.com";
String subject = "HAPPY";
String smtpHost = "smtp.qq.com";
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp"); // 使用的協(xié)議(JavaMail規(guī)范要求)
props.setProperty("mail.smtp.host", smtpHost); // 發(fā)件人的郵箱的 SMTP服務(wù)器地址
props.setProperty("mail.smtp.auth", "true"); // 請(qǐng)求認(rèn)證,參數(shù)名稱與具體實(shí)現(xiàn)有關(guān)
// 創(chuàng)建Session實(shí)例對(duì)象
Session session = Session.getDefaultInstance(props);
// 創(chuàng)建MimeMessage實(shí)例對(duì)象
MimeMessage message = new MimeMessage(session);
// 設(shè)置發(fā)件人
try {
message.setFrom(new InternetAddress(from));
// 設(shè)置收件人
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// 設(shè)置發(fā)送日期
message.setSentDate(new Date());
// 設(shè)置郵件主題
message.setSubject(subject);
// 設(shè)置純文本內(nèi)容的郵件正文
message.setText(body);
// 保存并生成最終的郵件內(nèi)容
message.saveChanges();
// 設(shè)置為debug模式, 可以查看詳細(xì)的發(fā)送 log
session.setDebug(true);
// 獲取Transport對(duì)象
Transport transport = session.getTransport("smtp");
// 第2個(gè)參數(shù)需要填寫(xiě)的是QQ郵箱的SMTP的授權(quán)碼,什么是授權(quán)碼,它又是如何設(shè)置?
transport.connect(from, "ipeiquufachheefg");
// 發(fā)送,message.getAllRecipients() 獲取到的是在創(chuàng)建郵件對(duì)象時(shí)添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
logger.info("發(fā)送完成");
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
idea?maven?經(jīng)常主目錄自動(dòng)變回默認(rèn)的解決方法
很多朋友反映idea?maven?經(jīng)常主目錄自動(dòng)變回默認(rèn),遇到這樣的問(wèn)題真的很頭疼,該如何解決呢?下面小編給大家介紹下idea?maven目錄變回默認(rèn)的解決方法,需要的朋友可以參考下2022-08-08
SpringBoot從繁至簡(jiǎn)的框架基礎(chǔ)教程
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開(kāi)發(fā)人員不再需要定義樣板化的配置2022-10-10
java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析
這篇文章主要為大家介紹了java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車的示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
springboot配置logback日志管理過(guò)程詳解
這篇文章主要介紹了springboot配置logback日志管理過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
IDEA卡在”正在解析Maven依賴項(xiàng)“的解決方法
在創(chuàng)建新的SpringBoot項(xiàng)目時(shí),始終卡在"正在解析Maven依賴項(xiàng)…",本文小編給大家介紹了幾種相關(guān)的解決方案,具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11
使用IntelliJ IDEA 進(jìn)行代碼對(duì)比的方法(兩種方法)
這篇文章給大家?guī)?lái)了兩種IntelliJ IDEA 進(jìn)行代碼對(duì)比的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01

