我正在尝试编写一个 csharp 应用程序来解析当电子邮件无法送达时发送的 NDR(未送达报告)。目标是从我们的邮件列表中取消订阅不再有效的电子邮件地址。
我已经设法编写了使用交换 Web 服务 API (EWS) 连接到我们的交换服务器的代码。抓取邮件正文,我想做的是匹配电子邮件地址和错误代码,以便我们可以生成电子邮件地址和错误报告以供人工审查。
消息正文的内容如下所示:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p><b><font color="#000066" size="3" face="Arial">Delivery has failed to these recipients or groups:</font></b></p>
<font color="#000000" size="2" face="Tahoma">
<p><a href="mailto:email@domain.com">email@domain.com</a><br>
A communication failure occurred during the delivery of this message. Please try resending the message later. If the problem continues, contact your helpdesk.<br>
</p>
</font><br>
<br>
<br>
<br>
<br>
<br>
<font color="#808080" size="2" face="Tahoma">
<p><b>Diagnostic information for administrators:</b></p>
<p>Generating server: WEB16.domain.net</p>
<p>email@domain.com<br>
#< #5.5.0 smtp;554 Sending address not accepted due to spam filter> #SMTP#</p>
<p>Original message headers:</p>
<pre>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+loq8SeaIzTsVoLE/3g==
From: <sender@domain.com>
To: <recipient@domain.com>
CC:
BCC:
Subject: domain.com order 178014 has been received and is pending
Date: Sun, 8 Jul 2012 09:27:56 -0400
Message-ID: <22C1C2F24E1744D4B51C1A5EF9DE3E50@domain.net>
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]
</pre>
</font>
</body>
</html>
我正在尝试提出一个正则表达式,该表达式将匹配发送到的电子邮件地址以及随附的错误消息。
// First we set the input string.
string body = message.Body.Text;
// Regex string
Regex emailregex = new Regex("^<p>(.+?)</a><br>$");
var match = emailregex.Match(body);
if (match.Success)
Console.WriteLine(match.Groups[1].Value);
// Regex string
Regex errorregex = new Regex("</a><br>\n(.+?)<br>$");
match = errorregex.Match(body);
if (match.Success)
Console.WriteLine(match.Groups[1].Value);
我设置的两个正则表达式似乎都不起作用。我不是一个正则表达式大师。谁能指出我做错的方向?
谢谢布拉德