Scott on Writing

Musings on technical writing...

Sending ELMAH Errors Via GMail

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 useSsl attribute 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>

posted on Thursday, May 21, 2009 12:34 PM

Feedback

# Dew Drop - May 22, 2009 | Alvin Ashcraft's Morning Dew 5/22/2009 4:47 AM Pingback/TrackBack

Dew Drop - May 22, 2009 | Alvin Ashcraft's Morning Dew

# re: Sending ELMAH Errors Via GMail 5/27/2009 5:17 AM Atif Aziz

Thanks for reporting and writing about this, Scott. BTW, you have a small typo in the closing <smtp> tag in the samples above. It reads </stmp> instead of </smtp>. Thought I'd point it out in case anyone is copying and pasting the configuration from your post.

# re: Sending ELMAH Errors Via GMail 5/28/2009 10:05 PM Steve Ewart

Scott, you are awesome! For me ELMAH stopped working around the same time that I upgraded to .NET 3.0 and I thought that that was the cause. I have tried countless things and this did the trick. I owe you a beer.... a case of beer!

# re: Sending ELMAH Errors Via GMail 8/11/2009 12:42 PM granadaCoder

Thanks!

Here is the same settings with the syntax error updates:

Note: Network was the wrong case.
Note: </stmp> should be </smtp>



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

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<February 2010>
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

Comment Stats

DayTotal% of Total
Sunday 2046.8%
Monday 42514.1%
Tuesday 51617.1%
Wednesday 55318.4%
Thursday 58019.2%
Friday 54718.2%
Saturday 1886.2%
Total 3013100.0%

Hour1Total% of Total
12:00 AM 772.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1806.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1876.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1063.5%
6:00 PM 1013.4%
7:00 PM 1073.6%
8:00 PM 923.1%
9:00 PM 882.9%
10:00 PM 893.0%
11:00 PM 953.2%
Total 3013100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.94158
Monday 4.80384
Tuesday 4.08477
Wednesday 7.47680
Thursday 6.25675
Friday 5.02462
Saturday 4.78177
Total 5.413013

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.02295
10:00 AM 5.63276
11:00 AM 4.20193
12:00 PM 6.14350
1:00 PM 3.17133
2:00 PM 5.00230
3:00 PM 7.62320
4:00 PM 4.00108
5:00 PM 6.04169
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.413013

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles