java讀取郵件excel附件的方法過程示例
java讀取excel附件的方法
項(xiàng)目中會(huì)遇到讀取郵件excel附件的信息至后臺,下面分享一個(gè)java讀取excel附件的方法。
1、要在后臺中讀取郵箱附件郵箱必須開啟IMAP服務(wù)
下圖示例為QQ郵箱開啟對應(yīng)服務(wù)的設(shè)置方法(其他郵箱也可找到對應(yīng)的設(shè)置):

按照提示開通對應(yīng)服務(wù),需要注意的是如果郵箱使用的是授權(quán)碼,則需要在后續(xù)使用時(shí)用授權(quán)碼代替密碼,授權(quán)碼授權(quán)方式更為安全。
2、添加依賴
收發(fā)郵件依賴jakarta.mail
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>{version}</version>
</dependency>讀取execel文件依賴POI
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>{version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>{version}</version>
</dependency>3、讀取接收到的郵件,并讀取附件信息
3.1讀取接收到的郵件代碼:
//設(shè)置郵箱服務(wù)器地址、端口、用戶
Properties props = new Properties();
props.setProperty("mail.imapStore.protocol","imap");
props.setProperty("mail.imap.host", "郵箱服務(wù)器host");
props.setProperty("mail.imap.port","郵箱服務(wù)端口");
// 是否啟用ssl
if (useSsl.equals(1)) {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.imap.ssl.enable", true);
props.put("mail.imap.ssl.socketFactory", sf);
props.put("mail.imap.ssl.protocols", "TLSv1.2");
}
Session session = Session.getInstance(props);
IMAPStore imapStore = (IMAPStore) session.getStore("imap");
// 輸入郵箱及密碼(授權(quán)碼)
imapStore.connect("郵箱服務(wù)器host", "郵箱服務(wù)端口", "email地址","email密碼(授權(quán)碼)");
//打開收件箱
IMAPFolder imapFolder = (IMAPFolder) imapStore.getFolder("INBOX");
if (!imapFolder.isOpen()) {
imapFolder.open(Folder.READ_ONLY);
}
//讀取指定時(shí)間之后的郵件數(shù)據(jù)
SearchTerm searchTerm = new ReceivedDateTerm(ComparisonTerm.GT, new Date(時(shí)間戳));
// 取到文件夾中所有信息
Message[] messages = imapFolder.search(searchTerm);
// 循環(huán)處理讀取到的郵件信息
for (Message message : messages) {
// 讀取附件
readAttachment(message )
}3.2讀取附件并且緩存代碼:
public List<EmailAttachmentPojo> readAttachment(Part part) throws Exception {
List<EmailAttachmentPojo> bodyParts = new LinkedList<>();
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent(); //復(fù)雜體郵件
//復(fù)雜體郵件包含多個(gè)郵件體
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
// 獲得復(fù)雜體郵件中其中一個(gè)郵件體
BodyPart bodyPart = multipart.getBodyPart(i);
// 某一個(gè)郵件體也有可能是由多個(gè)郵件體組成的復(fù)雜體
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
bodyParts.add(EmailAttachmentPojo.builder()
.bodyPart(bodyPart)
.filename(decodeText(bodyPart.getFileName()))
.build());
} else if (bodyPart.isMimeType("multipart/*")) {
bodyParts.addAll(readAttachment(bodyPart));
} else {
String contentType = bodyPart.getContentType();
if (contentType.contains("name") || contentType.contains("application")) {
bodyParts.add(EmailAttachmentPojo.builder()
.bodyPart(bodyPart)
.filename(decodeText(bodyPart.getFileName()))
.build());
}
}
}
} else if (part.isMimeType("message/rfc822")) {
bodyParts.addAll(readAttachment((Part) part.getContent()));
}
return bodyParts;
}3.3讀取附件excel里的內(nèi)容
使用POI讀取excel的步驟:
1.創(chuàng)建工作簿
2.獲取工作表
3.遍歷工作表獲得行對象
4.遍歷行對象獲取單元格對象
5.獲得單元格中的值
讀取excel內(nèi)容代碼示例:
// 1.獲取工作簿
XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\ghost\\Desktop\\hello.xlsx");
// 2.獲取工作表
// xlsx第一個(gè)工作簿(Sheet1),下標(biāo)從0開始,0就是第一個(gè)
XSSFSheet sheet = workbook.getSheetAt(0);
// 開始索引(0) 結(jié)束索引
int lastRowNum = sheet.getLastRowNum(); //行的結(jié)束索引
for (int i = 0; i <= lastRowNum; i++) {
// 拿到行
XSSFRow row = sheet.getRow(i);
if (row != null){
short cellNum = row.getLastCellNum(); //單元格的結(jié)束索引
for (int j = 0; j <= cellNum; j ++){
XSSFCell cell = row.getCell(j);
// 單元格不為空就去拿他的值
if (cell != null){
String stringCellValue = cell.getStringCellValue();
System.out.println(stringCellValue);
}
}
}
}
// 釋放資源
workbook.close();基于上述讀取郵件和讀取excel文件的方法就可以從郵箱中獲取到所需要的業(yè)務(wù)數(shù)據(jù),更多關(guān)于java讀取郵件附件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用@Validated和@Valid 解決list校驗(yàn)的問題
這篇文章主要介紹了使用@Validated和@Valid 解決list校驗(yàn)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java?Cookie與Session實(shí)現(xiàn)會(huì)話跟蹤詳解
session的工作原理和cookie非常類似,在cookie中存放一個(gè)sessionID,真實(shí)的數(shù)據(jù)存放在服務(wù)器端,客戶端每次發(fā)送請求的時(shí)候帶上sessionID,服務(wù)端根據(jù)sessionID進(jìn)行數(shù)據(jù)的響應(yīng)2022-11-11
java StringBuilder類的詳解及簡單實(shí)例
這篇文章主要介紹了java StringBuilder類的詳解及簡單實(shí)例的相關(guān)資料,實(shí)現(xiàn)了StringBuilder類的追加、插入、替換、刪除等操作,需要的朋友可以參考下2017-08-08
詳解springMVC之與json數(shù)據(jù)交互方法
本篇文章主要介紹了詳解springMVC之與json數(shù)據(jù)交互方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
如何在Spring中使用編碼方式動(dòng)態(tài)配置Bean詳解
這篇文章主要給大家介紹了關(guān)于如何在Spring中使用編碼方式動(dòng)態(tài)配置Bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
SpringMVC異常全局捕獲與錯(cuò)誤響應(yīng)的處理方法
編程式異常處理是通過在代碼中?顯式編寫異常捕獲邏輯(如?try-catch?塊)來管理異常的方式,開發(fā)者需要手動(dòng)處理每一個(gè)可能拋出異常的代碼段,本文給大家介紹SpringMVC異常全局捕獲與錯(cuò)誤響應(yīng)的處理方法,感興趣的朋友一起看看吧2025-03-03
MyEclipse2018中安裝Mybatis generator插件的實(shí)現(xiàn)步驟
這篇文章主要介紹了MyEclipse2018中安裝Mybatis generator插件的實(shí)現(xiàn)步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02

