1

I want to send notifications on:

  • Success
  • Failure
  • Delay

when I send my emails.

Unfortunately, it seems like the DeliveryNotificationOptions enumeration doesn't include an option to do all of these:

+---------------+----------------------------------------------------+
| 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               |
+---------------+----------------------------------------------------+

I'm not able to set more than 1 options like this:

 Msg.DeliveryNotificationOptions += DeliveryNotificationOptions.Delay;

What other options are there?

The best that I can think of would be to extend MailMessage with an IEnumerable of DeliveryNotificationOptions but I'm not quite sure how I'd use it.

Is this a viable way of doing this?

Ortund
  • 8,095
  • 18
  • 71
  • 139

1 Answers1

2

The DeliveryNotificationOptions enumeration has the Flags attribute applied to it, which indicates you can combine the values together:

var notifyOnFailureAndsuccess = 
    DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.OnSuccess

Using your code above, it's only a slight modification to get it to work:

Msg.DeliveryNotificationOptions |= DeliveryNotificationOptions.Delay;

See this question for discussion on how Flags actually works and what effect it has on your code.

DavidG
  • 113,891
  • 12
  • 217
  • 223
  • I had literally no idea. Thanks that's perfect :) – Ortund Jul 20 '17 at 12:40
  • 1
    Nitpick, but you can combine any enum. [The `[Flags]` attribute only applies to printing an enum's value](https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c). – CodeCaster Jul 20 '17 at 12:43
  • @CodeCaster that opens up so many new possibilities for me in my code. I always thought you get to pick item from the enum and thats it. Didn't allow for much flexibility but this... wow – Ortund Jul 20 '17 at 12:47
  • 1
    @CodeCaster Fair point, I was going to link to that question but for some reason decided not to. I also use the `Flags` attribute as an indicator that the consuming code will likely treat it as a combine-able enum. – DavidG Jul 20 '17 at 12:48
  • 1
    @Ortund well, you need to read the docs linked by David. You need to number your enum members in powers of two, otherwise the combined value will be ambiguous. E.g. when using 1, 2, 3, 4, 5 as enum values and you receive a 5, is it one 5, a 1+4, or a 2+3? – CodeCaster Jul 20 '17 at 12:48