我一直在计算已读、未读、自动回复、不在办公室、已回复已投递和未投递的邮件以及以下代码。但我想要邮件未投递的电子邮件 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;
您可能会发现此代码非常随意且面向目的,但要获得这么多,我也没有在线提供任何解决方案。