无法从电子邮件中读取自定义 header

Unable to read custom header from email

我在 Java 中发送电子邮件时正在添加自定义 header。如果有人回复该电子邮件,我需要将该电子邮件的内容存储在数据库中。我可能会收到该邮件的多个回复。因此,为了识别,我添加了具有唯一值的自定义 header。

从 imaps 商店的收件箱文件夹中读取未读电子邮件时,我无法读取自定义 header。除了自定义 header 我还能阅读其他 header.

我正在使用以下 Class

阅读电子邮件
public class OutLookReader_imaps {

    Folder inbox;
    public OutLookReader_imaps() throws IOException {
        System.out.println("Inside MailReader()...");
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.imaps.socketFactory.fallback", "false");
        props.setProperty("mail.imaps.port", "993");
        props.setProperty("mail.imaps.socketFactory.port", "993");
        props.put("mail.imaps.host", "outlook.office365.com");


        try {
            /* Create the session and get the store for read the mail. */

            Session session = Session.getDefaultInstance(System.getProperties(), null);
            Store store = session.getStore("imaps");

            store.connect("outlook.office365.com", 993, "example@entransys.com", "password");
            inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);            
            Flags seen = new Flags(Flags.Flag.SEEN);
            FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
            Message messages[] = inbox.search(unseenFlagTerm);
            if (messages.length == 0) System.out.println("No messages found.");

            System.out.println("No. of Unread Messages : " + inbox.getUnreadMessageCount());
            System.out.println("No. of Messages : " + inbox.getMessageCount());
            System.out.println("No. of Deleted Messages : " + inbox.getMode());

            for (int i = 0; i < messages.length; i++) {
              if (i > 10) {
                System.exit(0);
                inbox.close(true);
                store.close();
              }

              System.out.println("Message " + (i + 1));
              System.out.println("From : " + messages[i].getFrom()[0]);
              System.out.println("Subject : " + messages[i].getSubject());
              System.out.println("Sent Date : " + messages[i].getSentDate());
              System.out.println("Header : " + messages[i].getAllHeaders());
              System.out.println("Content : " + messages[i].getContent().toString());
              System.out.println("Content Type : " + messages[i].getContentType());
              System.out.println("Description : " + messages[i].getDescription());messages[i].getContent());         

              System.out.println("Unique Header : " + messages[i].getHeader("X-Universally-Unique-Identifier"));

              System.out.println("Header uni : " + messages[i].getHeader("X-Uniform-Type-Identifier"));  

              Enumeration allHeaders = messages[i].getAllHeaders();
                while (allHeaders.hasMoreElements()) {
                    Header header = (Header) allHeaders.nextElement();
                    String headerName = header.getName();
                    String headerVal = header.getValue();

                    System.out.println("Header Name : " + headerName);
                    System.out.println("Header Value : " + headerVal);
                }
            }         

            try {

                inbox.close(true);
                store.close();

            } catch (Exception ex) {
                System.out.println("Exception arise at the time of read mail");
                ex.printStackTrace();
            }

        } catch (MessagingException e) {
            System.out.println("Exception while connecting to server: " + e.getLocalizedMessage());
            e.printStackTrace();
            System.exit(2);
        }

    }

    public void printAllMessages(Message[] msgs) throws Exception {
        for (int i = 0; i < msgs.length; i++) {
            System.out.println("MESSAGE #" + (i + 1) + ":");
            printEnvelope(msgs[i]);
        }
    }

    public void printEnvelope(Message message) throws Exception {

        Address[] a;

        if ((a = message.getFrom()) != null) {
            for (int j = 0; j < a.length; j++) {
                System.out.println("Email From : " + a[j].toString());
            }
        }

        String subject = message.getSubject();

        Date receivedDate = message.getReceivedDate();
        Date sentDate = message.getSentDate(); 

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

        System.out.println("Email Subject : " + subject);

        if (receivedDate != null) {
            System.out.println("Received Date: " + df.format(receivedDate));
        }

        System.out.println("Sent Date : " + df.format(sentDate));
    }


    public static void main(String args[]) throws IOException {
        new OutLookReader_imaps();
    }

}

您添加到您发送的邮件中的自定义 header 不会添加到回复您发送的邮件的邮件中。通常会有一个 In-Reply-To header 或参考 header,其中包括您发送的消息的 Message-ID,但即使是那些也不能保证存在。将回复与已发送消息相关联的最可靠方法可能是在您发送的消息的主题中包含一个唯一 ID。