Sadaf Mansuri’s Blog

April 15, 2009

Elmah manual emailing

Filed under: Uncategorized — Tags: — admin @ 6:11 pm

In one of our project we are using Elmah. It is also using System.Net.Mail.MailMessage to send some emails. Elmah is sending emails on exceptions and at lot of places it is sending error emails manually. We decided to use uniform way to send emails. Its huge application not written by me so first I thought let me get from and to settings from web.config for Elmah and use it places where it sends email manually.

config file is having following settings

<sectionGroup name=”gotdotnet.elmah”>
<section name=”errorMail” type=”System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″/>

<errorMail from=”” to=”” subject=”Application Error” async=”true”/>

so I use following code to get from and to from config file.

Hashtable ht = (Hashtable)System.Web.Configuration.WebConfigurationManager.GetSection(”gotdotnet.elmah/errorMail”);
string from = ht["from"].ToString();
string to = ht["to"].ToString();

It is still not uniform way to send error emails. I started looking around how to log errors and send emails manually via Elmah. So I tried this code

Exception ex = new Exception(”Custom error message”);
GotDotNet.Elmah.Error Elmah_error = new GotDotNet.Elmah.Error(ex);

Now this is good for just looging. As per auther of Elmha, it won’t send emails.Check this link.

He as added this functionality in next version but I am not able to find that new version under version 2. So finally decided to throw unhandled exceptions so will get error emails through elmah.

Powered by WordPress