java - 来自未交付邮件的原始邮件标题

时间:2015-12-29 08:04:59

标签: java email javamail javax.mail

我必须获取未送达邮件发送的原始邮件的邮件标题。

目的:要检查回复是哪封邮件,请使用 In-Reply-To Message-ID 与其他邮件关联,以防万一未传递邮件的标题在标题中没有这样的 In-Reply-To 字段。 如果我能够获得原始邮件标题,我认为这个云可以解决。

到目前为止我所做的示例代码

  Properties props = System.getProperties();
                props.put("mail.smtp.host", AppProperties.getInstance().getEmailHostName());
                props.put("mail.smtp.socketFactory.port", AppProperties.getInstance().getEmailSmtpSocketFactoryPort());
                props.put("mail.smtp.socketFactory.class", AppProperties.getInstance().getEmailSMTPSocketFactoryClass());
                props.put("mail.smtp.auth", AppProperties.getInstance().getEmailSmtpAuth());
                props.put("mail.smtp.port", AppProperties.getInstance().getEmailSmtpSocketFactoryPort());
                String userid = AppProperties.getInstance().getEmailId();
                String password = AppProperties.getInstance().getEmailPassword();

                Session session = Session.getInstance(props, null);
                Store store = session.getStore("imaps");

                store.connect(props.getProperty("mail.smtp.host"), userid, password);

                Folder inbox = (Folder) store.getFolder("INBOX");
                inbox.open(Folder.READ_WRITE);
                FetchProfile profile = new FetchProfile();
                profile.add(FetchProfileItem.CONTENT_INFO);
                profile.add("X-mailer");
                Message[] messages = inbox.getMessages(inbox.getMessageCount() - 10, inbox.getMessageCount());
                inbox.fetch(messages, profile);


                for (Message message : messages) {

    System.out.println(message.getHeader("Message-ID")[0]);
    System.out.println(message.getHeader("In-Reply-To")[0]);

                }

                inbox.close(true);

                store.close();

1 个答案:

答案 0 :(得分:2)

如果你很幸运,那么'其他' smtp归还了他们。在这种情况下,您可以通过MultipartReport

访问它们
for (Message message : messages) {
 if(message instanceof MimeMessage) {
    MimeMessage mime = (MimeMessage)message;
    Object content = mime.getContent();
    if(content instanceof MultipartReport) {
       MultipartReport dsn = (MultipartReport)content;
       MimeMessage m = dsn.getReturnedMessage();
       if(m != null) {
          // Tadaa - headers are here:
          String originalMessageId = m.getMessageID();
       }
    }
 }
}