spring boot定時任務接收郵件并且存儲附件的方法講解
在spring boot中寫定時任務很簡單,兩個注解就可以實現(xiàn)。在啟動類中加@EnableScheduling ,然后在你需要定時的方法上加上@Scheduled(cron="0 10 8 * * ?");括號內(nèi)為cron表達式。如下圖:


接收郵件及其判斷是否有附件,并且存儲附件。
public class TimerTaskServiceImpl implements TimerTaskService {
@Autowired
private ParseTxtServiceImpl parseTxtService;
/**
* 接收郵件
*/
@Override
@Scheduled(cron="0 10 8 * * ?")
public void timerTaskInfo(){
//郵件配置信息
String host=Constants.MAIL_HOST;
String userName=Constants.MAIL_USER_NAME;
String passWord=Constants.MAIL_PASS_WORD;
//郵件配置類
Properties properties=new Properties();
//郵件配置緩存
Session session=Session.getDefaultInstance(properties);
session.setDebug(true);
String fileName=null;
try {
//郵件服務器的類型
Store store = session.getStore("pop3");
//連接郵箱服務器
store.connect(host,userName,passWord);
// 獲得用戶的郵件帳戶
Folder folder=store.getFolder("INBOX");
if (folder == null) {
logger.info("獲取郵箱文件信息為空");
}
// 設置對郵件帳戶的訪問權(quán)限可以讀寫
folder.open(Folder.READ_WRITE);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DATE,-1);
Date mondayDate = calendar.getTime();
SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.GT, mondayDate);
SearchTerm address=new SubjectTerm( "MU Report");
SearchTerm comparisonAndTerm = new AndTerm(address, comparisonTermLe);
Message[] messages = folder.search(comparisonAndTerm);
for(int i = 0 ; i < messages.length ; i++){
MimeMessage msg = (MimeMessage) messages[i];
//判斷是否有附件
boolean isContainerAttachment = isContainAttachment(msg);
if (isContainerAttachment) {
//保存附件
fileName=saveAttachment(msg,Constants.STORAGE_FILE);
//保存接收到的郵件并且收件箱刪除郵件
msg.setFlag(Flags.Flag.DELETED, true);
}
if(!isContainerAttachment) {
continue;
}
}
folder.close(true);
store.close();
parseTxtService.readTxt(fileName);
}catch (NoSuchProviderException e){
loggerError.error("接收郵箱信息異常:{}",e);
}catch (MessagingException e){
loggerError.error("連接郵箱服務器信息異常:{}",e);
}catch (IOException e){
loggerError.error("接收郵箱信息解析異常:{}",e);
}catch (IllegalStateException e){
loggerError.error("接收郵箱信息為空:{}",e);
}
}
/**
* 判斷郵件中是否包含附件
* @return 郵件中存在附件返回true,不存在返回false
* @throws MessagingException
* @throws IOException
*/
public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
boolean flag = false;
if (part.isMimeType(Constants.MULTI_PART)) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) ||
disp.equalsIgnoreCase(Part.INLINE))) {
flag = true;
} else if (bodyPart.isMimeType(Constants.MULTI_PART)) {
flag = isContainAttachment(bodyPart);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf(Constants.APPLICATION_CONTEXT) != -1) {
flag = true;
}
if (contentType.indexOf(Constants.NAME_CONTEXT) != -1) {
flag = true;
}
}
if (flag){
break;
}
}
} else if (part.isMimeType(Constants.MESSAGE_RFC)) {
flag = isContainAttachment((Part)part.getContent());
}
return flag;
}
/**
* 保存附件
* @param part 郵件中多個組合體中的其中一個組合體
* @param destDir 附件保存目錄
* @throws UnsupportedEncodingException
* @throws MessagingException
* @throws FileNotFoundException
* @throws IOException
*/
public String saveAttachment(Part part, String destDir) throws UnsupportedEncodingException,
MessagingException, FileNotFoundException, IOException {
String fileName=null;
if (part.isMimeType(Constants.MULTI_PART)) {
Multipart multipart = (Multipart) part.getContent(); //復雜體郵件
//復雜體郵件包含多個郵件體
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
//獲得復雜體郵件中其中一個郵件體
BodyPart bodyPart = multipart.getBodyPart(i);
//某一個郵件體也有可能是由多個郵件體組成的復雜體
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase
(Part.INLINE))) {
InputStream is = bodyPart.getInputStream();
saveFile(is, destDir, decodeText(bodyPart.getFileName()));
fileName=decodeText(bodyPart.getFileName());
} else if (bodyPart.isMimeType(Constants.MULTI_PART)) {
saveAttachment(bodyPart,destDir);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf(Constants.NAME_CONTEXT) != -1 || contentType.indexOf
(Constants.APPLICATION_CONTEXT) != -1) {
saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));
fileName=decodeText(bodyPart.getFileName());
}
}
}
} else if (part.isMimeType(Constants.MESSAGE_RFC)) {
saveAttachment((Part) part.getContent(),destDir);
}
return fileName;
}
/**
* 讀取輸入流中的數(shù)據(jù)保存至指定目錄
* @param is 輸入流
* @param fileName 文件名
* @param destDir 文件存儲目錄
* @throws FileNotFoundException
* @throws IOException
*/
private void saveFile(InputStream is, String destDir, String fileName)
throws FileNotFoundException, IOException {
BufferedInputStream bis = new BufferedInputStream(is);
if(fileName.contains(Constants.TXT_SUFIXX)) {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File(destDir + fileName)));
int len = -1;
while ((len = bis.read()) != -1) {
bos.write(len);
bos.flush();
}
bos.close();
bis.close();
}
}
}
其中查詢郵件的條件,你可以自行更改。

接收郵件服務器的配置

可能出現(xiàn)的bug
說說用模板可能會碰到的bug。
怎么回事呢?代碼寫了,看了好幾遍也沒錯,就是運行就報錯,在網(wǎng)上看了別人的代碼拿過來還是報錯,報錯如下:

這個錯誤大概意思就是我的模板的html中每個標簽都要是閉標簽,要這種類型的<a></a>,假如是<img xxx>這種標簽就會報錯。
如下所示,最坑的方法就是修改的,而且以后html的標簽都要是一對一對的,坑啊、

后來有找了很多資料,原來發(fā)現(xiàn)是這里,themeleaf默認應該是2.xx版本,這個版本解析標簽都要是一對一對的,到了3.xx之后,就不需要這么麻煩了!

都是版本問題
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- spring中通過ApplicationContext getBean獲取注入對象的方法實例
- Springboot-dubbo-fescar 阿里分布式事務的實現(xiàn)方法
- 詳解spring-boot下如何滿足多生產(chǎn)環(huán)境中個性化定制功能
- Spring 中優(yōu)雅的獲取泛型信息的方法
- spring @Transactional 無效的解決方案
- activemq整合springboot使用方法(個人微信小程序用)
- Springboot項目打war包docker包找不到resource下靜態(tài)資源的解決方案
- Spring自帶的校驗框架Validation的使用實例
- 詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑
- Spring通過ApplicationContext主動獲取bean的方法講解
相關(guān)文章
mybatis-plus返回map自動轉(zhuǎn)駝峰配置操作
這篇文章主要介紹了mybatis-plus返回map自動轉(zhuǎn)駝峰配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
SpringCloud_Sleuth分布式鏈路請求跟蹤的示例代碼
Spring Cloud Sleuth是一款針對Spring Cloud的分布式跟蹤工具,本文通過實例代碼介紹了SpringCloud_Sleuth分布式鏈路請求跟蹤,感興趣的朋友跟隨小編一起看看吧2023-02-02
springboot對象為null的屬性在json中不顯示的解決
這篇文章主要介紹了springboot對象為null的屬性在json中不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Java實現(xiàn)京東聯(lián)盟API數(shù)據(jù)獲取功能
這篇文章介紹了Java獲取京東聯(lián)盟API數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Intellij?IDEA創(chuàng)建web項目的超詳細步驟記錄
如果剛開始接觸IDEA,或者之前使用的是eclipse/myEclipse的話,即使是創(chuàng)建一個JAVA WEB項目,估計也讓很多人費了好幾個小時,下面這篇文章主要給大家介紹了關(guān)于Intellij?IDEA創(chuàng)建web項目的超詳細步驟,需要的朋友可以參考下2022-08-08

