ELMAH - Get SMTP credentials from Azure app settings

I have an Azure web application using ELMAH to log unhandled exceptions.

When I first deployed it, the full SMTP configuration was set in web.config, and ELMAH emailed exceptions:

<system.net>
    <mailSettings>
        <smtp from="me@mydomain.com">
            <network host="smtp.mailprovider.com"
                 port="123"
                 userName="myUserName"
                 password="p@ssw0rd" />
        </stmp>
    </mailSettings>
</system.net>

Since then, the username and password have been removed from web.config, and now they are saved as application settings configured through the Azure Portal.

Most of the emails that I send still work fine, as the email code can access these application settings and use them when creating an instance SmtpClient, for example:

var userName = WebConfigurationManager.AppSettings["smtp.userName"];
var password = WebConfigurationManager.AppSettings["smtp.password"];

var credentials = new NetworkCredential(userName, password);

using (var smtpClient = new SmtpClient { Credentials = credentials })
{
    await smtpClient.SendMailAsync(mailMessage);
}

What is the best way to force ELMAH to use credentials stored in application settings?

, :

+4
2

Elmah ErrorMailModule, , SendMail, Google.

Web.Config .


using System;
using System.Net.Mail;

namespace Test
{
    public class ErrorMailModule : Elmah.ErrorMailModule
    {
        protected override void SendMail(MailMessage mail)
        {
            if (mail == null) throw new ArgumentNullException(nameof(mail));

            // do what you want with the mail
            // (in my case this fires up the email service, which 
            // gets the credentials from the Azure settings)
        }
    }
}

-

, , - Elmah.ErrorLogModule, Elmah , Test.ErrorMailModule.

, ...

<system.web>
  <httpModules>
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
  </httpModules>
</system.web>
<system.webServer>
  <modules>
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
  </modules>
</system.webServer>

... :

<system.web>
  <httpModules>
    <add name="ErrorMail" type="Test.ErrorMailModule" />
  </httpModules>
</system.web>
<system.webServer>
  <modules>
    <add name="ErrorMail" type="Test.ErrorMailModule" preCondition="managedHandler" />
  </modules>
</system.webServer>

errorMail, Elmah - . :

<elmah>
  <errorMail from="user@domain.com" to="user@domain.com" subject="Custom Email Module"/>
</elmah>
+3

HTTP- , , IMO. Mailing , SmtpClient .

SMTP . , smtp , . .

, , smtp - web.config, . web.config:

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = configuration.GetSection("system.net/mailSettings/smtp") as SmtpSection;
section.Network.UserName = ConfigurationManager.AppSettings["myusername"];
section.Network.Password = ConfigurationManager.AppSettings["mypassword"];
configuration.Save();

web.config. , web.config . Azure.

+1

Source: https://habr.com/ru/post/1654037/


All Articles