williamsandonz
williamsandonz

Reputation: 16430

How to programmatically set recipient of Elmah email

I've got this method here, I wanted to go e.Mail.To = MAC, but apparently it's a read only property, which leaves me completely stumped on how I can programmatically set the recipients. Basically I want to change the to address based on my deployment level (live/test/dev) I also want to dispose() (not send) the email for dev/test modes.

Is there another way round this?

public static void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
        {
            if (!GlobalHelper.IsLiveMode)
            {
                e.Mail.Dispose();
            }
            else
            {
                MailAddressCollection MAC = new MailAddressCollection();
                MAC.Add("A");

            }

Upvotes: 6

Views: 610

Answers (1)

Parag Meshram
Parag Meshram

Reputation: 8521

Following snippet will solve your problem -

public static void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)         
{             
    if (!GlobalHelper.IsLiveMode)             
    {                 
        e.Mail.Dispose();             
    }             
    else         
    {                 
        MailAddressCollection MAC = new MailAddressCollection();                 
        MAC.Add("[email protected]");              
        MAC.Add("[email protected]");              



        e.Mail.To.Clear(); // Clears any existing mail addresses if you want to
        e.Mail.To.Add(MAC.ToString()); // To contains [email protected] & [email protected]
    } 
}

Upvotes: 5

Related Questions