2

我一直在计算已读、未读、自动回复、不在办公室、已回复已投递和未投递的邮件以及以下代码。但我想要邮件未投递的电子邮件 ID。

要获取未送达邮件的电子邮件,我可以通过解析该电子邮件的内容来完成,但这将是非常漫长的任务。请帮助我找到更好的解决方案

        JSONObject jo = new JSONObject();
        JSONArray dataCollection = new JSONArray();
        JSONObject data = null;
        try {

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

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

            Folder inbox = store.getFolder("inbox");
            inbox.open(Folder.READ_WRITE);
            int countDelivered = 0;
            int countUndelivered = 0;
            int countRead = 0;
            int countAutoReply = 0;
            int countUnRead = 0;
            int countReplied = 0;

            jo.put("TotalMessage", inbox.getMessageCount());

            Message[] messages = inbox.getMessages(0,50);

            for (Message message : messages) {

                boolean undelMsgs = message.getFrom()[0].toString().contains("Mail Delivery")
                        || message.getFrom()[0].toString().contains("Postmaster")
                        || message.getFrom()[0].toString().contains("Failed") 
                        || message.getFrom()[0].toString().contains("Failure")
                        || message.getSubject().contains("(Failure)")
                        || message.getSubject().contains("Undeliverable:");

                boolean readMsgs = message.getSubject().contains("Read:") 
                        && !message.getSubject().contains("Not read:")
                        && !message.getSubject().contains("Un-Read:") 
                        && !message.getSubject().contains("UnRead:") ;

                boolean unreadMsgs = message.getSubject().contains("Un-Read:") 
                        || message.getSubject().contains("UnRead:") 
                        || message.getSubject().contains("Not read:");

                boolean autoReply = message.getSubject().contains("Automatic") 
                        || message.getSubject().contains("Reply:") 
                        || message.getSubject().contains("[Auto-Reply]")
                        || message.getSubject().contains("Out of Office");

                boolean replied = message.getSubject().contains("RE:");
                data = new JSONObject();

                if (undelMsgs)
                    countUndelivered++;
                else
                    countDelivered++;

                if (readMsgs)
                    countRead++;
                if (unreadMsgs)
                    countUnRead++;

                if (autoReply)
                    countAutoReply++;

                if (replied)
                    countReplied++;
                data = new JSONObject();
                data.put("subject=", message.getAllHeaders());
                data.put("from=", message.getFrom()[0]);

                dataCollection.put(data);
            }

            inbox.close(true);
            System.out.println("Done....");
            store.close();
            data = new JSONObject();
            data.put("Delivered=", countDelivered);
            data.put("Undelivered=", countUndelivered);
            data.put("Read=", countRead);
            data.put("AutoReply=", countAutoReply);
            data.put("Replied=", countReplied);
            data.put("UnRead=", countUnRead);

            dataCollection.put(data);

            jo.put("tableData", dataCollection);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jo; 

您可能会发现此代码非常随意且面向目的,但要获得这么多,我也没有在线提供任何解决方案。

4

1 回答 1

1

可悲的是,没有黄金法则。这取决于失败的 SMTP 服务器选择将什么发回给您。

但有希望:MultipartReport

如果您在邮件中保留了此内容,并且另一个 SMTP 选择添加(至少是邮件头)原始邮件,您可以通过以下方式访问它:

  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) {
              String originalMessageId = m.getMessageID();
               //TADAA!
           }
        }
     }
  }

在所有其他情况下,您必须求助于其他想法。

  • MultiPartReport.getReport() 可能是 DeliveryNotification 并且标头可能包含带有您的消息 ID 的 original-envelope-id-field。

  • 也许有一个“回复”标题?

  • 也许有一个“参考”标题?

两者都可能包含您的原始消息 ID。但甚至可能没有设置,因此您只能解析报告文本本身。

于 2015-12-19T13:59:27.610 回答