简体   繁体   English

从 .eml 文件中获取文本的最佳方法是什么?

[英]What is the best way to get text from .eml file?

I try to get to, from, topic and message body from several eml files which are on my local drive.我尝试从我本地驱动器上的几个 eml 文件访问主题和消息正文。 Now I've tried to use Apache Commons Email, but sometimes it loops with no errors.现在我尝试使用 Apache Commons Email,但有时它会循环播放而没有错误。 Here is my code which supposed to get text from eml and save it to txt:这是我的代码,它应该从 eml 获取文本并将其保存到 txt:

            MimeMessage mimeMessage = MimeMessageUtils.createMimeMessage(null, file);
            MimeMessageParser parser = new MimeMessageParser(mimeMessage);

            if (parser.parse().hasPlainContent()) {
                //Trying to get text of the message
                try (FileWriter writer = new FileWriter(txtName)) {
                    writeHeaders(writer, parser);
                    writer.write(parser.parse().getPlainContent());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (parser.parse().hasHtmlContent()) {
                try (FileWriter writer = new FileWriter(txtName)) {
                    writeHeaders(writer, parser);
                    String text = Jsoup.parse(parser.parse().getHtmlContent()).text();
                    writer.write(text);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

Also here is writeHeaders method:这里还有 writeHeaders 方法:

    private void writeHeaders(FileWriter writer, MimeMessageParser parser) throws Exception {
        writer.write("From :" + parser.getFrom() + "\n");
        writer.write("To:" + parser.getTo() + "\n");
        writer.write("Subject:" + parser.getSubject() + "\n");
        writer.write("Message:" + "\n" + "\n");
    }

And here is method to get attachments:这是获取附件的方法:

          if (parser.parse().hasAttachments()) {
                //Getting and saving attachments from eml
                List<DataSource> attachments = parser.parse().getAttachmentList();
                for (DataSource attachment : attachments) {
                    if (attachment.getName() != null && !attachment.getName().isEmpty()) {
                        try (InputStream is = attachment.getInputStream()) {
                            File save = new File(saveDir + File.separator + attachment.getName());
                            FileOutputStream fos = new FileOutputStream(save);
                            byte[] buf = new byte[4096];
                            int bytesRead;
                            while ((bytesRead = is.read(buf)) != -1) {
                                fos.write(buf, 0, bytesRead);
                            }
                            fos.close();
                            if (save.getName().endsWith("eml")) {
                                parseEml(save, count);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

So, maybe there are any easier ways to get text and attachments?那么,也许有更简单的方法来获取文本和附件?

Yes much easier.是的要容易得多。 Simple Java Mail (Github) can read .eml files and makes the content very accessible. Simple Java Mail (Github) 可以 读取 .eml 文件并使内容非常易于访问。 If you find something like a looping error there too (unlikely), I'll be happy to assist you there (I actively maintain Simple Java Mail):如果您在那里也发现了类似循环错误的东西(不太可能),我很乐意在那里为您提供帮助(我积极维护 Simple Java Mail):

Email email = EmailConverter.emlToEmail(emlFile);

email.getFromRecipient();
email.getSubject();
email.getPlainText();
email.getHTMLText();
email.getAttachments();
email.getEmbeddedImages();
email.getHeaders();
// etc. etc.

Also supports S/MIME encrypted emails (if you have the required certificates to decrypt the emails).还支持 S/MIME 加密电子邮件(如果您有解密电子邮件所需的证书)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 json 文本文件加载 JSONObject 的最佳方法是什么? - What’s the best way to load a JSONObject from a json text file? 在Spring中从属性文件获取值的最佳方法是什么? - What is the Best way to get the values from the Properties File in Spring? 拆分此文本文件的最佳方法是什么? - What's the best way to split this text file? 用Java标记文本文件的最佳方法是什么? - What is the best way to tokenize a text file in Java? 检查文本文件中的字符串是否等于JTextField中的字符串的最佳方法是什么? - What is the best way to check if a string from a text file equals what's in JTextField? 如何从Java中的.eml文件获取内部主题 - how to get inner subject from .eml file in java 从文本文件读取字符,而忽略换行符的多余空格的最佳方法是什么? - What is the best way to read in chars from a text file while ignoring extra spaces from new lines? 将图块网格保存到Java游戏的文本文件中的最佳方法是什么? - What is the best way of saving a tile grid to text file for a java game? 在Java中一次读取两行文本文件的最佳方法是什么? - What is the best way to read a text file two lines at a time in Java? 在 Java 中以字节为单位获取文本大小的最佳方法是什么? - What is the best way to get the size of text in bytes in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM