我正在尝试使用Java Api将电子邮件消息写入.eml文件。写入文件后,我需要使用'SHA1'算法来验证文件是否已成功下载。我尝试了以下操作。

1)我已将电子邮件写入.eml文件,下载完成后,我使用message.getInpuStream()接收了相同邮件的输入流。现在我使用此流并将.eml文件的流进行验证。现在它失败了。

2)我只是通过将使用.eml拍摄的InputStream复制到某些message.getInpuStream() .eml's来编写FileOutputStream。我再次传递了两者的输入流,以使用SHA1进行验证。现在验证已成功,但是当我打开下载的.eml文件时,它仅显示正文内容,而不显示任何附件以及发件人和收件人信息。

所以我想知道使用.eml将邮件消息写入message.writeTo()与使用message.getInputStream有什么区别吗?

任何建议或信息表示赞赏。

以下是我正在尝试的代码。

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


        String host = "pop.gmail.com";
        String username = "xxxx@gmail.com";
        String password = "password";
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "pop3s");
        Session session = Session.getInstance(new Properties(), null);
        Store store = session.getStore("imaps");
        store.connect(host, username, password);

         Folder folder = store.getFolder("INBOX");
         folder.open(Folder.READ_WRITE);

        Message message[] = folder.getMessages();
        OutputStream out=null;
        InputStream inStream=null;
        File file=null;

        for (int i = 0; i < 20; i++) {

            inStream=message[i].getInputStream();
            file=new File("E:/MailTest/mail-"+i+".msg");
            out=new FileOutputStream(file);

            IOUtils.copy(inStream, out);

            IOUtils.closeQuietly(inStream);
            IOUtils.closeQuietly(out);

            System.out.println("Validating the File ...!!!");

            InputStream srcInStream=message[i].getInputStream();
            InputStream tgtStream=new FileInputStream(file);
            EmailHelper helper=new EmailHelper();
            //calling method to validate the file
            boolean validate=helper.fileValidation(srcInStream, tgtStream, "SHA1");
            if(validate){
                System.out.println("Validation Successful...!!");
            }else{
                System.out.println("Validation Failed..!!");
            }
            tgtStream.close();
            srcInStream.close();
            }

        folder.close(false);
        store.close();
    }
}


提前致谢

最佳答案

writeTo方法同时包含标头和内容。 getInputStream方法仅返回内容。如果需要同时包含标头和内容的InputStream,请将其写入文件,然后再读回,或者使用PipedInputStream和线程。

07-24 09:36
查看更多