Frage

When adding multiple recipients to a MailMessage.BCC there is no option for AddRange(). Only MailMessage.Bcc.Add();

Can this functionality be changed by an extension method? I'm a little lost at this point, any pointers would be very much appreciated.

War es hilfreich?

Lösung

Supposing you are talking about the System.Net.Mail.MailMessage class, what you need is already provided by the MailAddressCollection.Add method (Bcc is of the MailAddressCollection type).

Just call Add method with multiple e-mail addresses separated by a comma character (",").

Check this:

http://msdn.microsoft.com/en-us/library/ms144695(v=vs.100).aspx

Andere Tipps

MailMessage.Bcc is of type MailAddressCollection. This MailAddressCollection implements ICollection<MailAddress>. So what you can do, is write a generic AddRange extension method which applies to any ICollection<T>.

This will look like the following:

public static class CollectionExtensions
{
    public static void AddRange<T>(this ICollection<T> target, IEnumerable<T> items)
    {
        foreach(var item in items)
        {
            target.Add(item);
        }   
    }
}

You can then use this like so:

var address1 = new MailAddress("abc@xyz.com");
var address2 = new MailAddress("wxy@hij.com");
message.Bcc.AddRange(new[] { address1, address2 });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top