我一直在为包含以下代码的Read-Reciept,NotRead,AutoReplied,Out of Office,Relied Delivered和未送达邮件的电子邮件进行计数。但我想要邮件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;
你可能会发现这个代码非常狂热且面向目的但是为了得到这么多,我也没有在线提供任何解决方案。
答案 0 :(得分:1)
但是有希望: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,Headers可能包含带有您的消息ID的original-envelope-id-field。
也许有一个“In-Reply-To”标题?
两者都可以包含您的原始Message-ID。但可能甚至没有设置,所以你要解决报告文本本身。