最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java讀取郵件excel附件的方法過程示例

 更新時間:2023年10月07日 15:35:11   作者:光法V3  
這篇文章主要介紹了java讀取郵件excel附件的方法過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

java讀取excel附件的方法

項目中會遇到讀取郵件excel附件的信息至后臺,下面分享一個java讀取excel附件的方法。

1、要在后臺中讀取郵箱附件郵箱必須開啟IMAP服務

下圖示例為QQ郵箱開啟對應服務的設置方法(其他郵箱也可找到對應的設置):

按照提示開通對應服務,需要注意的是如果郵箱使用的是授權碼,則需要在后續(xù)使用時用授權碼代替密碼,授權碼授權方式更為安全。

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");
// 輸入郵箱及密碼(授權碼)
imapStore.connect("郵箱服務器host", "郵箱服務端口", "email地址","email密碼(授權碼)");
//打開收件箱
IMAPFolder imapFolder = (IMAPFolder) imapStore.getFolder("INBOX");
if (!imapFolder.isOpen()) {
   imapFolder.open(Folder.READ_ONLY);
}
//讀取指定時間之后的郵件數據
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里的內容

使用POI讀取excel的步驟:

1.創(chuàng)建工作簿

2.獲取工作表

3.遍歷工作表獲得行對象

4.遍歷行對象獲取單元格對象

5.獲得單元格中的值

讀取excel內容代碼示例:

// 1.獲取工作簿
XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\ghost\\Desktop\\hello.xlsx");
// 2.獲取工作表
// xlsx第一個工作簿(Sheet1),下標從0開始,0就是第一個
XSSFSheet sheet = workbook.getSheetAt(0);
// 開始索引(0) 結束索引
int lastRowNum = sheet.getLastRowNum(); //行的結束索引
for (int i = 0; i <= lastRowNum; i++) {
    // 拿到行
    XSSFRow row = sheet.getRow(i);
    if (row != null){
        short cellNum = row.getLastCellNum(); //單元格的結束索引
        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è)務數據,更多關于java讀取郵件附件的資料請關注腳本之家其它相關文章!

相關文章

最新評論

南昌市| 前郭尔| 当雄县| 滨州市| 中江县| 柳江县| 基隆市| 扎兰屯市| 阿坝县| 客服| 哈密市| 清徐县| 呼伦贝尔市| 西充县| 老河口市| 海南省| 辽阳市| 东丽区| 如皋市| 义马市| 沁源县| 府谷县| 乳源| 许昌县| 山东| 罗江县| 汶上县| 灌南县| 普安县| 东海县| 巴塘县| 抚宁县| 商洛市| 龙井市| 兴宁市| 徐州市| 邮箱| 邹城市| 名山县| 长乐市| 金秀|