Sending ELMAH Errors Via GMail

Posted by: Scott on Writing, on 21 May 2009 | View original | Bookmarked: 0 time(s)

ELMAH is a free, open source error logging system for ASP.NET created by Atif Aziz. This blog post assumes the reader is already familiar with using and configuring ELMAH. If this is not the case, refer to Simone Busoli's article, ELMAH - Error Logging Modules and Handlers for more information.

One of ELMAH's most useful features is that it can automatically e-mail the details of a runtime error to a specified set of recipients. This feature allows developers to be notified immediately once a runtime error occurs. (ELMAH can also syndicate recent errors as an RSS feed and, with the release of ELMAH version 1.0, ELMAH can even tweet error details.) While I've used this feature many times in the past, I ran into some difficulties setting it up to send the error e-mails through GMail's SMTP servers. You can specify the SMTP settings directly in ELMAH's <errorMail> setting or you can define it in the <system.net> section, as described in Sending Email in ASP.NET. I typically use the <system.net> setting because I also use this information in my website and don't want to repeat it twice in Web.config. Consequently, my Web.config file looks similar to the following:

<elmah>
<errorMail
from="..."
to="..."
subject="..."
async="true"
/>
</elmah>

<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network host="..." port="..." userName="..." password="..." />
</stmp>
</mailSettings>
</system.net>

The first challenge is that GMail's SMTP servers require SSL. However, you cannot specify SSL behavior through the <system.net> settings; rather, you have to do it when you instantiate the SmtpClient object, via its EnableSsl property. To instruct ELMAH to send e-mail via SSL you need to set the <errorMail> section's useSslattribute to true, like so:

<errorMail
...
useSsl="true" />

As of the time I am writing this blog post, this attribute is not shown in the sample Web.config file, so you wouldn't know it exists unless you examined ELMAH's source code.

The second issue is that GMail's SMTP server uses port 587 instead of the standard port 25. I had correctly set the port number in the <system.net> section and believed I could then omit it from the <errorMail> section. However, I was wrong. If you omit the port from <errorMail> then ELMAH uses port 25. It does not turn to the <system.net> section and use the port number specified there.

To remedy this you can do one of two things:

  • In <errorMail>, set the smtpPort attribute to the port you want to use.
  • In <errorMail>, set the smtpPort attribute to "0". Doing so will cause ELMAH to use the port defined per the <system.net> settings.

With these changes my Web.config ends up looking like the following:

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

/>
</elmah>

<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network host="smtp.gmail.com"
port="587"
userName="..."
password="..." />
</stmp>
</mailSettings>
</system.net>

Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 5389 | Hits: 34

Similar Posts

  • Deleting All Records In a Table EXCEPT For the N Most Recently Added Records more
  • Keeping ELMAH's Error Log Size In Check more
  • The Technology Post for May 22nd more
  • Validation - Part 3 - Server-Side more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD