title: Trying to match email address and error message in non-delivery report
body: <p>I'm attempting to write a csharp app which parses NDRs (non-delivery reports) sent when an email is un-deliverable. The goal is to unsubscribe email addresses from our mailing lists which are no longer valid.</p>
  
  <p>I've managed to write code which connects to our exchange server using the exchange web services API (EWS). Grabs the message body and what I want to do is match the email address and error code so that we can generate a report of email addresses and errors for manual review.</p>
  
  <p>The content of the body of a message appears as such:</p>
  
  <pre><code>    &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;p&gt;&lt;b&gt;&lt;font color="#000066" size="3" face="Arial"&gt;Delivery has failed to these recipients or groups:&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;
      &lt;font color="#000000" size="2" face="Tahoma"&gt;
      &lt;p&gt;&lt;a href="mailto:email@domain.com"&gt;email@domain.com&lt;/a&gt;&lt;br&gt;
      A communication failure occurred during the delivery of this message. Please try resending the message later. If the problem continues, contact your helpdesk.&lt;br&gt;
      &lt;/p&gt;
      &lt;/font&gt;&lt;br&gt;
      &lt;br&gt;
      &lt;br&gt;
      &lt;br&gt;
      &lt;br&gt;
      &lt;br&gt;
      &lt;font color="#808080" size="2" face="Tahoma"&gt;
      &lt;p&gt;&lt;b&gt;Diagnostic information for administrators:&lt;/b&gt;&lt;/p&gt;
      &lt;p&gt;Generating server: WEB16.domain.net&lt;/p&gt;
      &lt;p&gt;email@domain.com&lt;br&gt;
      #&amp;lt; #5.5.0 smtp;554 Sending address not accepted due to spam filter&amp;gt; #SMTP#&lt;/p&gt;
      &lt;p&gt;Original message headers:&lt;/p&gt;
      &lt;pre&gt;Received: from WEB12 ([192.168.33.64]) by WEB16.domain.net with Microsoft
       SMTPSVC(7.5.7601.17514);    Sun, 8 Jul 2012 09:27:42 -0400
      Thread-Topic: domain.com order 178014 has been received and is pending
      thread-index: Ac1dDXyHxyN&amp;#43;loq8SeaIzTsVoLE/3g==
      From: &amp;lt;sender@domain.com&amp;gt;
      To: &amp;lt;recipient@domain.com&amp;gt;
      CC:
      BCC:
      Subject: domain.com order 178014 has been received and is pending
      Date: Sun, 8 Jul 2012 09:27:56 -0400
      Message-ID: &amp;lt;22C1C2F24E1744D4B51C1A5EF9DE3E50@domain.net&amp;gt;
      MIME-Version: 1.0
      Content-Type: text/plain
      Content-Transfer-Encoding: 7bit
      X-Mailer: Microsoft CDO for Windows 2000
      Content-Class: urn:content-classes:message
      Importance: normal
      Priority: normal
      X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.17609
      Return-Path: sender@domain.com
      X-OriginalArrivalTime: 08 Jul 2012 13:27:42.0955 (UTC) FILETIME=[743A53B0:01CD5D0D]
      &lt;/pre&gt;
      &lt;/font&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>I am trying to come up with a regular expression that will match the email address that was sent to, and the accompanying error message.</p>
  
  <pre><code>            // First we set the input string.
              string body = message.Body.Text;
  
              // Regex string
              Regex emailregex = new Regex("^&lt;p&gt;(.+?)&lt;/a&gt;&lt;br&gt;$");
  
              var match = emailregex.Match(body);
  
              if (match.Success)
                   Console.WriteLine(match.Groups[1].Value);
  
              // Regex string
              Regex errorregex = new Regex("&lt;/a&gt;&lt;br&gt;\n(.+?)&lt;br&gt;$");
  
              match = errorregex.Match(body);
  
              if (match.Success)
                  Console.WriteLine(match.Groups[1].Value);
  </code></pre>
  
  <p>Neither of the regular expressions I setup appear to not be working. I'm not much of a regular expression guru. Can anyone point me in the direction of what I'm doing wrong? </p>
  
  <p>Thanks
  Brad</p>