java 从目录中读取并解析.eml文件

xzlaal3s  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(270)

我有大约6000+未送达的Outlook电子邮件在一个目录
我需要读取目录中的每一个eml文件,并提取原始的。eml,这是一个附件,并将其保存在一个新的文件夹中。我怎么能做到这一点,而不依赖于任何第三方库使用Java(如只使用JDK中可用的类)
我是新手,不知道如何进行这一点。任何帮助将不胜感激。TIA!
我已经尝试了下面的代码,但缺少一些东西。我该如何修复这个问题?

import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.Part;
import jakarta.mail.Session;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;

import java.io.*;
import java.util.Properties;

public class EMLParser {

    public static void main(String args[]) throws Exception {

        Properties props = new Properties();
        Session mailSession = Session.getDefaultInstance(props, null);

        InputStream is = new FileInputStream("/Users/Documents/old/Test1.eml");
        MimeMessage message = new MimeMessage(null, is);
        System.out.println("Subject : " + message.getSubject());
        System.out.println("From : " + message.getFrom()[0]);
        System.out.println("--------------");
        System.out.println("Body : " + message.getContent());
        String contentType = message.getContentType();
        if (contentType.contains("multipart")) {
            System.out.println("Multipart Email File");
            Multipart multiPart = (Multipart) message.getContent();
            int numberOfParts = multiPart.getCount();
            System.out.println("Parts:::" + numberOfParts);
            String saveDir = "/Users/vijayd/Documents/Temp/";
            saveDir = saveDir + File.separator;
            System.out.println(saveDir);
            boolean file = new File(saveDir).mkdir();
            if (file) {
                System.out.println("Directory: created");

            }

            for (int partCount = 0; partCount < numberOfParts; partCount++) {
                MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                String disposition = part.getDisposition();
                if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                    // this part is attachment
                    String fileName = part.getFileName();
                    String extension = "";
                    System.out.println("Attached File Name::" + fileName);
                    saveDir = saveDir + File.separator + fileName;
                    int i = fileName.lastIndexOf(".");
                    if (i > 0) {
                        extension = fileName.substring(i + 1);
                        System.out.println("Extension:" + extension);
                    }
                    if (extension.equalsIgnoreCase("eml")) {
                        part.saveFile(saveDir);
                        extractEML(saveDir, null);
                        System.out.println("This is a eml file");
                    } else if (extension.equalsIgnoreCase("msg")) {
                        part.saveFile(saveDir);
                        extractEML(saveDir, null);
                        System.out.println("This is a msg file");
                    } else {
                        System.out.println("This is other file");
                    }
                } else {
                    System.out.println("Not an eml file");
                    System.out.println("File Name::" + part.getFileName());
                }
            }
        }
    }

    public static void extractEML(String emlPath, String wi) throws MessagingException, IOException, FileNotFoundException, MessagingException {
        //String fileName="";
        String path = System.getProperty("user.dir") + File.separator + "Docs" + File.separator + wi + File.separator + "EML_PDF";
        boolean file = new File(path).mkdir();
        if (file) {
            System.out.println("Folder EML_PDF Created Successfully");
        }
        Properties props = new Properties();
        Session mailSession = Session.getDefaultInstance(props, null);
        InputStream source = new FileInputStream(emlPath);
        MimeMessage message = new MimeMessage(mailSession, source);
        System.out.println("Subject : " + message.getSubject());
        System.out.println("From : " + message.getFrom()[0]);
        System.out.println("--------------");
        System.out.println("Body : " + message.getContent());
        String contentType = message.getContentType();
        if (contentType.contains("multipart")) {
            System.out.println("Multipart EMail File");
            Multipart multiPart = (Multipart) message.getContent();
            int numberOfParts = multiPart.getCount();
            System.out.println("Parts:::" + numberOfParts);
            for (int partCount = 0; partCount < numberOfParts; partCount++) {
                MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                    String fileName = part.getFileName();
                    String extension = "";
                    path = path + File.separator + fileName;
                    int i = fileName.lastIndexOf(".");
                    if (i > 0) {
                        extension = fileName.substring(i + 1);
                    }
                    if (extension.equalsIgnoreCase("pdf")) {
                        System.out.println("Path:" + path);
                        part.saveFile(path);
                    }
                }
            }
        }
    }

}
eaf3rand

eaf3rand1#

您可以使用Jakarta Mail API,它是JDK中包含的Java Mail API的一部分。

import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.Part;
import jakarta.mail.Session;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class EMLParser {

    public static void main(String args[]) throws Exception {
        Properties props = new Properties();
        Session mailSession = Session.getDefaultInstance(props, null);

        File folder = new File("/Users/Documents/old/");
        File[] listOfFiles = folder.listFiles();

        for (File file : listOfFiles) {
            if (file.isFile() && file.getName().endsWith(".eml")) {
                InputStream is = new FileInputStream(file.getAbsolutePath());
                MimeMessage message = new MimeMessage(mailSession, is);
                String contentType = message.getContentType();

                if (contentType.contains("multipart")) {
                    Multipart multiPart = (Multipart) message.getContent();
                    int numberOfParts = multiPart.getCount();

                    for (int partCount = 0; partCount < numberOfParts; partCount++) {
                        MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                        String disposition = part.getDisposition();
                        if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                            // this part is attachment
                            String fileName = part.getFileName();
                            if (fileName.endsWith(".eml")) {
                                String saveDir = "/Users/vijayd/Documents/Temp/";
                                File dir = new File(saveDir);
                                if (!dir.exists()) {
                                    dir.mkdir();
                                }
                                saveDir = saveDir + File.separator + fileName;
                                try (InputStream in = part.getInputStream()) {
                                    Files.copy(in, Paths.get(saveDir));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

相关问题