Reputation: 338
I have been doing count for emails which are Read-Reciept,NotRead,AutoReplied,Out of Office,Replied Delivered and undelivered mails with following code.But I want email ids on which the mails are undelivered.
To get email for undelivered mails I can do it by parsing the content for that email but that will be very lengthy task.Please help me with a better solution
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;
You may find this code very willy and purpose oriented but to get this much also I didn't have any solution available online.
Upvotes: 2
Views: 1559
Reputation: 13858
Sadly, there's no golden Rule for that. It depends on what the failing SMTP server choses to send you back.
But there's hope: MultipartReport
If you get a hold of this inside your message and the other SMTP chose to add (at least the headers of) the original message, you could access it this way:
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!
}
}
}
}
In all other cases you have to resort to other ideas.
The MultiPartReport.getReport() might be a DeliveryNotification and the Headers might contain a original-envelope-id-field with your message id.
Maybe there's an "In-Reply-To" Header?
Both could contain your original Message-ID. But might not even be set, so you'd be down to parsing the report text itself.
Upvotes: 1