Elmah core send email

Elmah Core is a popular open-source error logging and exception handling framework for.NET applications. To send an email notification when an error occurs, you can configure Elmah Core to use a custom email provider. Here's a step-by-step guide to help you achieve this:

Prerequisites

Step 1: Configure Elmah Core

In your web.config file, add the following configuration settings:

<configuration>
  <appSettings>
    <add key="elmah.errorMailTo" value="[email protected]" />
    <add key="elmah.errorMailSubject" value="Error occurred in {ApplicationName}" />
  </appSettings>
  <elmah>
    <errorMail from="[email protected]" to="[email protected]" subject="Error occurred in {ApplicationName}" />
  </elmah>
</configuration>

Replace [email protected] with your actual email address and SMTP server settings.

Step 2: Create a custom email provider

Create a new class that inherits from Elmah.ErrorMail and overrides the Send method. This method will be responsible for sending the email notification:

using Elmah;
using System.Net.Mail;

public class CustomEmailProvider : ErrorMail
{
    public override void Send(Error error)
    {
        using (var mail = new MailMessage())
        {
            mail.From = new MailAddress("[email protected]");
            mail.To.Add("[email protected]");
            mail.Subject = error.Subject;
            mail.Body = error.Message;

            using (var client = new SmtpClient())
            {
                client.Send(mail);
            }
        }
    }
}

Step 3: Register the custom email provider

In your web.config file, add the following configuration setting:

<configuration>
  <appSettings>
    <add key="elmah.errorMailProvider" value="YourNamespace.CustomEmailProvider, YourAssembly" />
  </appSettings>
</configuration>

Replace YourNamespace and YourAssembly with the actual namespace and assembly name of your custom email provider class.

Step 4: Test the email notification

Restart your application and simulate an error by throwing an exception or using a tool like Fiddler to send a bad request to your application. Elmah Core should send an email notification to the configured email address with the error details.

That's it! You should now receive email notifications when errors occur in your.NET application using Elmah Core.