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

问题描述 投票:1回答:2

我正在尝试检测我的应用程序发送的电子邮件是否发送失败。这意味着我没有尝试验证电子邮件,但实际上只是它到达目标邮箱。

这是我正在使用的.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?

    }   

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

谢谢!

c# .net exchangewebservices
2个回答
0
投票

如果您在文件中的电子邮件地址无效,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);

    }
}

你可以找到其他的例子here


0
投票

我正在尝试检测我的应用程序发送的电子邮件是否发送失败。这意味着我没有尝试验证电子邮件,但实际上只是它到达目标邮箱。

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

最终,Exchange将使远程服务器接受该消息,否则它将放弃。

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

许多服务器只接受并丢弃发送给不存在的用户的邮件。其他人将他们扔进一个全球“垃圾邮件”文件夹。

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.