如何为单个MailMessage设置多个DeliveryNotificationOptions?

时间:2017-07-20 12:33:48

标签: c# smtpclient mailmessage

我想发送通知:

  • 成功
  • 失败
  • 延迟

当我发送电子邮件时。

不幸的是,似乎DeliveryNotificationOptions枚举不包含执行所有这些操作的选项:

+---------------+----------------------------------------------------+
| Member Name   | Description                                        |
+---------------+----------------------------------------------------+
| Delay         | Notify if the delivery is delayed.                 |
+---------------+----------------------------------------------------+
| Never         | A notification should not be generated under       |
|               | any circumstances.                                 |
+---------------+----------------------------------------------------+
| None          | No notification information will be sent. The mail |
|               | server will utilize its configured behavior to     |
|               | determine whether it should generate a delivery    |
|               | notification.                                      |
+---------------+----------------------------------------------------+
| OnFailure     | Notify if the delivery is unsuccessful.            |
+---------------+----------------------------------------------------+
| OnSuccess     | Notify if the delivery is successful               |
+---------------+----------------------------------------------------+

我无法设置超过1个这样的选项:

 Msg.DeliveryNotificationOptions += DeliveryNotificationOptions.Delay;

还有哪些其他选择?

我能想到的最好的方法是使用IEnumerable的DeliveryNotificationOptions扩展MailMessage,但我不太清楚我是如何使用它的。

这是一种可行的方法吗?

1 个答案:

答案 0 :(得分:2)

DeliveryNotificationOptions枚举已应用Flags属性,表示您可以将值组合在一起:

var notifyOnFailureAndsuccess = 
    DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess

使用上面的代码,只需稍作修改即可使其正常工作:

Msg.DeliveryNotificationOptions |= DeliveryNotificationOptions.Delay;

有关Flags实际工作原理以及它对您的代码有何影响的讨论,请参阅this question