Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");

            }
like image 725
williamsandonz Avatar asked Aug 20 '12 02:08

williamsandonz


1 Answers

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]
    } 
}
like image 143
Parag Meshram Avatar answered Nov 15 '22 08:11

Parag Meshram