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

How to set multiple DeliveryNotificationOptions for a single 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;

还有哪些其他选择?

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

这样做可行吗?

DeliveryNotificationOptions 枚举应用了 Flags 属性,这表明您可以将这些值组合在一起:

var notifyOnFailureAndsuccess = 
    DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess

使用上面的代码,只需稍作修改即可运行:

Msg.DeliveryNotificationOptions |= DeliveryNotificationOptions.Delay;

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