90

Is anyone using Elmah to send exceptions via email? I've got Elmah logging set up via SQL Server, and can view the errors page via the Elmah.axd page, but I am unable to get the email component working. The idea here is to get the email notification so we can react more quickly to exceptions. Here is my web.config (unnecessary sectionss omitted), with all the sensitive data replaced by * * *. Even though I am specifying a server to connect to, does the SMTP service need to be running on the local machine?

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="elmah">
            <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
            <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
            <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
        </sectionGroup>
    </configSections>
    <appSettings/>
    <connectionStrings>
        <add name="elmah-sql" connectionString="Data Source=***;Initial Catalog=***;Persist Security Info=True;User ID=***;Password=***" />
    </connectionStrings>

    <elmah>
        <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sql"   >
        </errorLog>
        <errorMail from="test@test.com"
           to="test@test.com"
           subject="Application Exception"
           async="false"
           smtpPort="25"
           smtpServer="***"
           userName="***"
           password="***">
        </errorMail>
    </elmah>

    <system.web>        
        <customErrors mode="RemoteOnly" defaultRedirect="CustomError.aspx">
            <error statusCode="403" redirect="NotAuthorized.aspx" />
            <!--<error statusCode="404" redirect="FileNotFound.htm" />-->
        </customErrors>
        <httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
            <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
        </httpHandlers>
        <httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
        </httpModules>
    </system.web>

</configuration>
Mark Struzinski
  • 32,945
  • 35
  • 107
  • 137

3 Answers3

79

You need the ErrorMail httpModule.

add this line inside the <httpModules> section

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

If you're using a remote SMTP server (which it looks like you are) you don't need SMTP on the server.

Michael Haren
  • 105,752
  • 40
  • 168
  • 205
TonyB
  • 3,882
  • 2
  • 25
  • 22
68

Yes, if you are not using remote SMTP server you must have SMTP Server configured locally. You can also configure email for elmah in web.config as follows:

<configSections>
   <sectionGroup name="elmah">
     <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler,  Elmah"> 
    </sectionGroup>
</configSections> 

<elmah> 
     <errorMail from="from Mail Address" to="to mail address" 
                async="true"  smtpPort="0" useSsl="true" /> 
</elmah>

<system.net> 
    <mailSettings> 
      <smtp deliveryMethod ="Network"> 
        <network host="smtp.gmail.com" port="587" userName="yourgmailEmailAddress"   password="yourGmailEmailPassword" /> 
      </smtp> 
    </mailSettings> 
</system.net>
Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
Anand Patel
  • 739
  • 6
  • 9
  • 4
    This worked great. I didn't realize that ELMAH can hook in to the standard mailSettings section. The smtpPort="0" attribute is kind of strange. – Ben Mills Apr 09 '12 at 14:57
  • 1
    smtpPort="0" does not seem to be needed as it uses the port specified under system.net; while running with the development server I had to set useSsl="false" or else it would crash the server (probably cause I did not set up SSL) – Răzvan Flavius Panda Oct 02 '13 at 09:34
  • I found the order of elements to be significant. Specifying system.net AFTER elmah FAILED. The ELMAH system acted like no mail server was defined. Putting system.net first made it work. – Mark Meuer Nov 18 '15 at 19:06
  • If I use above settings, anyone access to the web.config file will know my email password right? – Unbreakable May 08 '17 at 04:08
5

I have used Elmah myself in this configuration and I had to setup the server with SMTP locally. It is a straight-forward install on you local IIS server. This should do the trick.

Good point above, you need the errorMail module BUT if you are not using a remote SMTP server you need SMTP locally, just to clarify.

Rob Bazinet
  • 1,020
  • 1
  • 8
  • 12