【问题标题】:How to set multiple DeliveryNotificationOptions for a single MailMessage?如何为单个 MailMessage 设置多个 DeliveryNotificationOptions?
【发布时间】:2017-12-26 03:51:10
【问题描述】:

我想发送通知:

  • 成功
  • 失败
  • 延迟

当我发送电子邮件时。

不幸的是,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,但我不太确定如何使用它。

这是一种可行的方法吗?

【问题讨论】:

    标签: c# smtpclient mailmessage


    【解决方案1】:

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

    var notifyOnFailureAndsuccess = 
        DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess
    

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

    Msg.DeliveryNotificationOptions |= DeliveryNotificationOptions.Delay;
    

    请参阅this question,了解Flags 的实际工作原理以及它对您的代码的影响。

    【讨论】:

    • 我真的不知道。谢谢,这是完美的:)
    • Nitpick,但您可以组合任何枚举。 The [Flags] attribute only applies to printing an enum's value.
    • @CodeCaster 在我的代码中为我开辟了许多新的可能性。我一直认为您可以从枚举中选择项目,仅此而已。没有太多的灵活性,但是这......哇
    • @CodeCaster 公平点,我打算链接到那个问题,但由于某种原因决定不这样做。我还使用 Flags 属性作为指示消费代码可能会将其视为可组合枚举。
    • @Ortund 好吧,您需要阅读 David 链接的文档。您需要以 2 的幂为枚举成员编号,否则组合值将不明确。例如。当使用 1、2、3、4、5 作为枚举值并收到 5 时,是 5、1+4 还是 2+3?
    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 2019-03-26
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多