java - 未送达邮件的原始邮件 header

标签 java email jakarta-mail

我必须获取从未送达的邮件发送的原始邮件的邮件 header 。

目的:检查回复的是哪封邮件,对于其他邮件,我使用 In-Reply-ToMessage-ID 来关联它们,但以防万一对于未送达的邮件, header 会发生变化, header 中没有这样的 In-Reply-To 字段。 如果我能够获得原始消息 header ,我认为这个问题就可以解决。

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

  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();

最佳答案

如果幸运的话,“其他”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();
       }
    }
 }
}

关于java - 未送达邮件的原始邮件 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34506996/

相关文章:

Java - 数组 - 检查同一输入中的重复项

html - 为什么发送邮件时不在另一个div的底部添加一个div?

HTML 电子邮件多个 cid 引用不显示在 hotmail 上

java - 捕获 Javamail 异常

java - 关于 JVM 创建

java - 沿元素将列表拆分为子列表

java - 无法使 kie-drools-workbench-6.2 在 tomcat 7 上工作

python - 如何使用 Mimemultipart 在 Python 中发送邮件

java - 无法使用 SMTP 发送电子邮件(获取 javax.mail.MessagingException : Could not convert socket to TLS;)

Java Mail 较大的 base64 图像在 Linux 中具有换行符,导致它们在 Apple Mail.app 等客户端中失败