How to programmatically set recipient of Elmah email
asp.net, asp.net-mvc, elmah
Solution
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("A@XYZ.COM");
MAC.Add("B@XYZ.COM");
e.Mail.To.Clear(); // Clears any existing mail addresses if you want to
e.Mail.To.Add(MAC.ToString()); // To contains A@XYZ.COM & B@XYZ.COM
}
}
Problem
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"); } ```