Find message by MessageId

0 votes
asked Mar 30, 2011 by Enrico 1 (170 points)
edited Jul 12, 2012

Hi, i'have the below question.

I send an mail with an attached file to an invalid sender.

After that i received a mail related to a not delivery report.

The problem is. I need to read the attach file in order to get some information.

The questions are: I noticed that in the reply message i have all message i sent. Can i read the attach file from the notification delivery mail?

I noticed that i've the MessageId from the original mail. Can i find a mail using MessageId as filter criteria?

Best Regards Enrico

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Mar 31, 2011 by Lukas Pokorny (109,270 points)
edited Jul 12, 2012
 
Best answer

1) Once you have a MailMessage object which contains the non-delivery report, you can use the following code to extract the original message from it:

        MailMessage ndr = ...

MailMessage original = null;
        foreach (Attachment att in ndr.Attachments)
        {
            switch (att.ContentType.MediaType)
            {
                case "text/rfc822-headers":
                    original = new MailMessage();
                    original.Options |= MimeOptions.OnlyParseHeaders;
                    original.Load(att.GetContentStream());
                    break;

case "message/rfc822":
                    original = att.ContentMessage;
                    break;
            }
        }

Please note that not all non-delivery reports contain the full original message. Some only contain its headers and some don't contain it at all.

2) We have written a simple class that makes it easy to parse the delivery status part of a non-delivery report get it from. (The code is written in C# but if you prefer VB.NET, let us know and we will convert it for you). Using it in custom applications is simple - for example:

        MailMessage ndr = ...

DeliveryStatus status = DeliveryStatus.ParseMessage(ndr);
        if (status != null)
        {
            // ... the message is an NDR/DSN, do something with it
        }
        else
        {
            // ... the message is an ordinary e-mail, do something else with it
        }

The class parses both delivery status notification messages (which includes NDR messages) and disposition notification messages that are similar. Use DeliveryStatus object's OriginalMessageId to get the MessageId of the original message.

3) What do you mean by "find a mail using MessageId as filter criteria", exactly? Do you mean using Imap object's Search method? If that's the case, I don't think that's possible, unfortunately. However, you can at least search for all delivery status notification messages using the following code:

        Imap client = new Imap();
        ...

client.SelectFolder("Inbox");

ImapMessageCollection messages = client.Search(ImapSearchParameter.Header("Content-type", "multipart/report"));

Additionally, you can add a ImapSearchParameter.Sent(...) filter to only search for messages that arrived recently.

...