get the undeliverable email address with mimekit/mailkit library

Error processing SSI file

Answers

  1. Marchetti

    • 2021/8/5

    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? 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 (e.g. 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.

  2. Ryan

    • 2021/3/21

    If you look at the raw message source, does the value of the Content-Type header match something along the lines of multipart/report; 

  3. Eric

    • 2016/9/19

    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).

  4. Vance

    • 2020/8/17

    the machine readable description of the bounce. this is a message/delivery-status part. the original message that bounced. this part is optional, and might not 

  5. Judah

    • 2020/9/3

    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.

  6. Emir

    • 2019/1/2

    Since you probably only care about the individual email addresses on the From header of the message, you could just use this: foreach ( var mailbox in message. From. Mailboxes ) Console. WriteLine ( "{0}'s email address is {1}", mailbox. Name, mailbox.

  7. Nasir

    • 2017/4/15

    LAST UPDATED: OCTOBER 17 2020 - This is a quick example of how to send an email in ASP.NET Core 3.1 using the MailKit email client library.

  8. Dawson

    • 2016/2/23

    If you are asking how to know which message the delivery status is in reference to, then you need to check the StatusGroups property of the MessageDeliveryStatus and check for the Original-Envelope-Id field. e.g. like this: foreach ( var statusGroup in messageDeliveryStatus.

  9. Gonzales

    • 2020/6/3

    Find out most popular NuGet mail Packages. NET 4.5+ using MailKit. NET mail-client library that is based on MimeKit and optimized for mobile devices 

  10. Mordechai

    • 2021/5/15

    For example, I retrieve an email (with embedded image) in my asp.net application using mailkit and store it in my database. Afterwards if I forward the email (in my web application) to a gmail address, what should be done to show the embedded images? Now I only see this in the forwarded email:

  11. Malcolm

    • 2015/1/8

    MailKit is an Open Source cross-platform .NET mail-client library that is based on MimeKit and optimized for mobile devices. Features include: * HTTP, Socks4, 

  12. Clayton

    • 2017/6/27

    MailKit 2.5.2 (2020-03-14) Added work-around for ENVELOPE responses with a NIL address token in an address-list. (issue #991) MailKit 2.5.1 (2020-02-15) Fixed the IMAP ENVELOPE parser to have a more lenient fallback if it fails to be able to parse the Message-Id token value. (issue #976)

  13. Arlo

    • 2016/1/25

    Different people mean different things when they say "forward", so I guess I'll provide answers to the different meanings that I can think of.

  14. Evan

    • 2015/7/17

    Here's how you can download summary information instead of full messages (and then use said summary info to get the stuff you want): var items = client.Inbox.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope); foreach (var item in items) { // here's how to get the subject string

  15. Ruggiero

    • 2021/3/23

    NET MIME parsers, but it's also orders of magnitude faster. Parse gigabytes of mail in seconds. Download Source code. Mobile Ready. MimeKit and MailKit are 

  16. Watson

    • 2016/5/26

    The first thing that you will need to do is to configure your GMail account to enable less secure apps, or you'll need to use OAuth 2.0 authentication (which is a bit more complex). Then, assuming that your GMail account is user@gmail.com, you would use the following code snippet to connect to GMail via IMAP: C#.

  17. Calum

    • 2016/5/16

    Imap has no vulnerabilities reported, and its dependent libraries have no get the undeliverable email address with mimekit/mailkit library.

  18. Zaiden

    • 2021/7/24

    You can then parse the messages to look for bounced emails using a free Mime parsing library like C# Mime Parser, MimeKit or SharpMimeTools 

Comments are closed.

More Posts