News & Views.

The latest news, web designs and digital projects happening at Storm.

Sharing is caring:

Setup email alerts from ELMAH when exceptions are raised

Adam Pope Profile Image

Adam Pope
June 4, 2010

This is part of a series of posts on using ELMAH to handle error logging for your ASP.NET web applications.  We’ve already looked at how to get started with ELMAH and how to allow remote access to your error logs.  In this post we are going to look at how to configure ELMAH to send you an email alert every time an exception occurs on your site.  This is a really handy feature if you want to catch bugs early and keep your site running smoothly.

ELMAH makes sending email alerts really quite simple.  Open up your web.config file and add an errorMail section definition and corresponding errorMail entry as follows:

	<sectionGroup name="elmah">
		<section name="errorLog"
                               type="Elmah.ErrorLogSectionHandler, Elmah"/>
		<section name="errorMail"
                                type="Elmah.ErrorMailSectionHandler, Elmah"/>

	<errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="..."/>
	<errorMail from="" to=""/>

You’ll also need to add a reference to the Elmah.ErrorMailModule in your httpModules section.

		<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
		<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>

You will then need to configure an SMTP server for in your web.config.  You can do this in two ways: inline on the errorMail definition, or in a mailSettings section. Examples of both are shown below. Personally, I prefer to use a mailSettings section as I use the SMTP details elsewhere in my applications.

		<smtp deliveryMethod="network">
			<network host="..." port="25" userName="..." password="..." />


<errorMail from="..." to="..."  async="true" smtpServer="..." smtpPort="25" userName="..." password="..." />

If you want to send your error emails using GMail’s SMTP server you will have to change a couple of bits in your configuration. GMail requires SSL to be enabled. We cannot specify this through the section so we need to add the useSsl parameter to the errorMail tag. We also need to specify port 587. If you don’t specify a port on your errorMail tag, ELMAH will use port 25 by defualt. To tell it to use the port we have defined in the section, set smtpPort="0". [Thanks to Scott Mitchell for this tip]

	<errorMail from="..." to="..." async="true" smtpPort="0" useSsl="true" />

		<smtp deliveryMethod="network">
			<network host=""
				 password="..." />

That’s all you need.  I suggest creating a page with a deliberate error on to check that everything is set up correctly.

What Next?  Well, if you have a team of developers working on a site then you probably have bug tracking software keeping track of problems that need working on.  Most popular bug tracking packages allow you to post issues by email – why not pipe your error report emails straight into your bug tracker, after all problems that are happening now should be top of your todo list (See Jeff Atwood’s Exception-Driven Development).

Caution: If you experience email issues (maybe a password changes, or an SMTP relay is misconfigured) then you will not receive emails alerting you to the problem (or any other problems).  This is a catch-22 that has caught me out a few times.   If this becomes a problem for you, ELMAH also provides an RSS feed of errors which you could subscribe to, it can also Tweet your errors to you!