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

按照提示開通對應服務,需要注意的是如果郵箱使用的是授權(quán)碼,則需要在后續(xù)使用時用授權(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讀取接收到的郵件代碼:
//設置郵箱服務器地址、端口、用戶
Properties props = new Properties();
props.setProperty("mail.imapStore.protocol","imap");
props.setProperty("mail.imap.host", "郵箱服務器host");
props.setProperty("mail.imap.port","郵箱服務端口");
// 是否啟用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("郵箱服務器host", "郵箱服務端口", "email地址","email密碼(授權(quán)碼)");
//打開收件箱
IMAPFolder imapFolder = (IMAPFolder) imapStore.getFolder("INBOX");
if (!imapFolder.isOpen()) {
imapFolder.open(Folder.READ_ONLY);
}
//讀取指定時間之后的郵件數(shù)據(jù)
SearchTerm searchTerm = new ReceivedDateTerm(ComparisonTerm.GT, new Date(時間戳));
// 取到文件夾中所有信息
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(); //復雜體郵件
//復雜體郵件包含多個郵件體
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
// 獲得復雜體郵件中其中一個郵件體
BodyPart bodyPart = multipart.getBodyPart(i);
// 某一個郵件體也有可能是由多個郵件體組成的復雜體
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第一個工作簿(Sheet1),下標從0開始,0就是第一個
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è)務數(shù)據(jù),更多關(guān)于java讀取郵件附件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用@Validated和@Valid 解決list校驗的問題
這篇文章主要介紹了使用@Validated和@Valid 解決list校驗的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java?Cookie與Session實現(xiàn)會話跟蹤詳解
session的工作原理和cookie非常類似,在cookie中存放一個sessionID,真實的數(shù)據(jù)存放在服務器端,客戶端每次發(fā)送請求的時候帶上sessionID,服務端根據(jù)sessionID進行數(shù)據(jù)的響應2022-11-11
詳解springMVC之與json數(shù)據(jù)交互方法
本篇文章主要介紹了詳解springMVC之與json數(shù)據(jù)交互方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
如何在Spring中使用編碼方式動態(tài)配置Bean詳解
這篇文章主要給大家介紹了關(guān)于如何在Spring中使用編碼方式動態(tài)配置Bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-05-05
MyEclipse2018中安裝Mybatis generator插件的實現(xiàn)步驟
這篇文章主要介紹了MyEclipse2018中安裝Mybatis generator插件的實現(xiàn)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02

