简体   繁体   中英

get the undeliverable email address with mimekit/mailkit library

I started using @jstedfast Mimekit/Mailkit library , which is great by the way, to pull undeliverables using its subject line for search. I tried using the message.to, message.resentto.

how do i get that information. My first try today. I was able to get the list and the body but I just need the email. I tried using s22.imap but there's no support anymore then I discovered this. I know you're active here @jstedfast I need you help..there's no discussion tab in your github.

Thanks in advance

If you look at the raw message source, does the value of the Content-Type header match something along the lines of multipart/report; report-type=delivery-status multipart/report; report-type=delivery-status ? If so, it is very likely that this message will have a sub-part with a Content-Type header with a value of message/delivery-status . It should be the second child part of the multipart/report (the first part should be a human-readable explanation).

If so, you can cast the message/delivery-status MimeEntity to an instance of a MessageDeliveryStatus . You'll notice that the MessageDeliveryStatus class has a property called StatusGroups which is a list of multiple collections of headers (eg a list of HeaderList objects).

Each HeaderList contains information about a particular recipient that failed. I think what you want to do is look at the Final-Recipient header which will contain 2 pieces of information:

  1. The address-type (should typically be "rfc822" )
  2. The mailbox of the recipient (which is what you want)

Unfortunately, MimeKit does not have any tools to parse the Final-Recipient header, but it should be trivial to locate the end of the address-type parameter in the header value by using IndexOf (';') and then you can use something like MailboxAddress.TryParse() to parse it (or you could probably just Substring() the value w/o parsing).

So, the way this might look in code is this:

string[] GetUndeliverableAddresses (MimeMessage message)
{
    var report = message.Body as MultipartReport;

    if (report == null)
        throw new ArgumentException ("This is not a multipart/report!");

    MessageDeliveryStatus status = null;

    for (int i = 0; i < report.Count; i++) {
        status = report[i] as MessageDeliveryStatus;
        if (status != null)
            break;
    }

    if (status == null)
        throw new ArgumentException ("Did not contain a message/delivery-status!");

    var undeliverables = new List<string> ();
    for (int i = 0; i < status.StatusGroups.Count; i++) {
        var recipient = status.StatusGroups[i]["Final-Recipient"];
        int semicolon = recipient.IndexOf (';');

        var undeliverable = recipient.Substring (semicolon + 1).Trim ();
        undeliverables.Add (undeliverable);
    }

    return undeliverables.ToArray ();
}

For more information on message/delivery-status , see https://tools.ietf.org/html/rfc3464

Hope that helps.

this is what I did

 foreach (var uid in inbox.Search(query))
        {
            var message = inbox.GetMessage(uid);
           // Console.WriteLine("Subject: {0}", message.Subject);
            //Console.WriteLine("Subject: {0}", message.Headers);
           // Console.WriteLine("Subject: {0}", message.BodyParts);
            var text = message.BodyParts;
            string src = text.ElementAt(1).ToString();
            int srcStart = src.IndexOf("RFC822;",0); << i used final-recipient intially
            int srcEnd   = src.IndexOf("Action", 0);
            Console.WriteLine("Email:" + src.Substring(srcStart + 8, srcEnd - (srcStart + 8)));
            Console.WriteLine(src);


            //foreach (var part in message.BodyParts)
            //{
            //    Console.WriteLine(part);
            //    // do something
            //}

        }

let me know if there could be a problem..will I get the rfc822 if the recipient inbox is full? there's no way i can test that.. I tested with emails with nonexistent domain, mailbox does not exist..with live.com, randomdomain.com,yahoo.com and gmail. gmail, on the other hand does not return any undeliverables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM