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

时间:2019-04-20 07:18:19

标签: java javamail mime-message

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

从imaps存储中的收件箱文件夹中读取未读电子邮件时,我无法读取自定义标头。除了自定义标头之外,我还可以读取其他标头。

我正在使用以下课程阅读电子邮件

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

}

1 个答案:

答案 0 :(得分:2)

添加到发送的邮件中的

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