如何在 Exchange C# 上检测发送电子邮件失败



我正在尝试检测我的应用程序发送的电子邮件是否传递失败。这意味着我不是在尝试验证电子邮件,而只是在它到达目标邮箱时。

这是我正在使用的.Net Framework应用程序代码:

public void Send(EmailRequest emailRequest)
{
// Creates the message itselft
EmailMessage message = new EmailMessage(ServiceExchange);
message.Body = new MessageBody(emailRequest.Message);
message.Subject = emailRequest.Subject;
message.ToRecipients.Add(emailRequest.To);
// Create a custom extended property and add it to the message. 
Guid myPropertySetId = Guid.NewGuid();
ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "MyExtendedPropertyName", MapiPropertyType.String);
message.SetExtendedProperty(myExtendedPropertyDefinition, "MyExtendedPropertyValue");
// Asynchronously, call
message.SendAndSaveCopy();
// Wait one second (while EWS sends and saves the message). 
System.Threading.Thread.Sleep(1000);
// Now, find the saved copy of the message by using the custom extended property. 
ItemView view = new ItemView(5);
SearchFilter searchFilter = new SearchFilter.IsEqualTo(myExtendedPropertyDefinition, "MyExtendedPropertyValue");
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, myExtendedPropertyDefinition);
FindItemsResults<Item> findResults = ServiceExchange.FindItems(WellKnownFolderName.SentItems, searchFilter, view);
if (findResults == null) throw new Exception("Could not find results - findResults is null");
if (findResults.Items == null) throw new Exception("Could not find results - findResults.Items is null");
if (findResults.Items.Count == 0) throw new Exception("Could not find results - findResults.Items is 0");
if (findResults.Items.Count > 1) throw new Exception("findResults items returned more than one result");
ItemId itemID = null;
// TODO change this to single line
// Process results and retrieve the email item ID (unique)
foreach (Item myItem in findResults.Items)
{
if (myItem is EmailMessage)
{
EmailMessage em = myItem as EmailMessage;
itemID = em.Id;
//Console.WriteLine(em.Subject);
//Console.WriteLine(em.Id.UniqueId);
}
}
// TODO: ANY WAY TO RETRIEVE EMAIL FAILURE?
}   

例如:如果我将其发送到有效的电子邮件,一切都很好。这里的目标是检测故障,以便我们可以联系企业并检查为什么我们存储了错误的电子邮件地址。

谢谢!

如果您存档的电子邮件地址无效,SendAndSaveCopy(( 应该抛出一个异常来说明这一点。您可以实现 try/catch 语句来检测任何故障,并将失败发送到记录器,这将为您提供一个文本列表:

public void Send(EmailRequest emailRequest)
{
try
{
// Creates the message itselft
EmailMessage message = new EmailMessage(ServiceExchange);
message.Body = new MessageBody(emailRequest.Message);
message.Subject = emailRequest.Subject;
message.ToRecipients.Add(emailRequest.To);
// Create a custom extended property and add it to the message. 
Guid myPropertySetId = Guid.NewGuid();
ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "MyExtendedPropertyName", MapiPropertyType.String);
message.SetExtendedProperty(myExtendedPropertyDefinition, "MyExtendedPropertyValue");
// Asynchronously, call
message.SendAndSaveCopy();
// Wait one second (while EWS sends and saves the message). 
System.Threading.Thread.Sleep(1000);
}
catch (Exception x)
{
logger.LogError(x.Message);
}
}

您可以在此处找到其他示例。

我正在尝试检测我的应用程序发送的电子邮件是否传递失败。这意味着我不是在尝试验证电子邮件,而只是在它到达目标邮箱时。

将邮件交给 Exchange 后,它甚至可能不会立即发出。Exchange 尝试发送后,可能会发现远程服务器不可用,然后稍后重试。

最终,Exchange 要么让远程服务器接受邮件,要么放弃。

如果远程服务器接受邮件,则在邮件中配置标志时,您可以获得发送回执。但是,这并不意味着邮件实际上已传递给收件人或收件人阅读了它;这仅意味着 Exchange 能够将其提供给目标服务器。

许多服务器只是接受和丢弃发送给不存在的用户的邮件。其他人将它们扔进全局"垃圾邮件"文件夹。

如果在邮件中设置了正确的"答复"地址,则当邮件无法在 Exchange 管理员设置的超时时间内传递到远程服务器时,您将收到来自 Exchange 的电子邮件。这通常需要几天。

但是,即使您设置了"已读回执"标志,收件人也完全有可能阅读邮件而不发送邮件。合规是自愿的。

真正非常简短的答案是"您可以检测到即时故障,但实际上无法确定消息是否已被读取或传递",并且在任何情况下都不可能发生在您的代码中。

如果有帮助,可以立即检测到一些错误,这些错误将显示在您的 try/catch 块中,但这些是例外而不是规则。

最新更新