ELMAH-从Azure应用程序设置获取SMTP凭据 [英] ELMAH - Get SMTP credentials from Azure Application Settings

查看:75
本文介绍了ELMAH-从Azure应用程序设置获取SMTP凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure Web App,它使用 ELMAH 记录未处理的异常。

I've got an Azure Web App using ELMAH to log unhandled exceptions.

当我第一次部署它时,web.config中定义了完整的SMTP设置,并且ELMAH通过电子邮件发送了例外:

When I first deployed it, the web.config had the full SMTP setup defined in it, 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>

此后,用户名和密码已从web.config中删除,并且现在存储为应用程序设置,通过Azure门户进行配置。

The username and password have since been removed from the web.config, and they're now stored as application settings, configured through the Azure Portal.

我发送的大多数电子邮件仍然可以正常工作,因为电子邮件代码可以访问这些应用程序设置并在实例化 SmtpClient ,例如:

Most of the emails I send still work fine, as the email code can access these application settings and use them when instantiating the SmtpClient, e.g.:

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);
}

让ELMAH使用存储在应用程序设置中的凭据的最佳方法是什么?

What's the best way to get ELMAH to use the credentials stored in the application settings?

我可以看到的选项:


  • 有一个页面解释了如何使用ELMAH的ErrorTweetModule进行HTTP表单发布,并附有错误详细信息到任何URL。接收该帖子的控制器然后可以使用存储的凭据通过电子邮件发送详细信息。

  • WebBase具有指向文章建议您可以直接将电子邮件发送至没有身份验证的收件人的SMTP服务器,但是如果您设置了DomainKeys,它说这可能不起作用。

  • answer 链接到文章关于拦截邮件事件,以自定义消息。

  • There is a page on the wiki explaining how to use ELMAH's ErrorTweetModule to do an HTTP form post with the error details to any URL. The controller receiving the post could then use the stored credentials to email the details on.
  • The WebBase has a link to an article suggesting you can send emails directly to the recipient's SMTP server without authentication, but it says this may not work if you have DomainKeys set up, which I do.
  • This answer links to an article about intercepting the Mailing event, to customise the message.

推荐答案

我最终创建了一个自定义Elmah的 ErrorMailModule 版本,从标准版本派生而来,但根据Atif Aziz的一些建议,覆盖了 SendMail 方法在关于Google网上论坛的讨论中。

I ended up creating a custom version of Elmah's ErrorMailModule, derived from the standard one, but overriding the SendMail method, based on some advice from Atif Aziz in a discussion on Google Groups.

唯一需要做的更改是创建新模块,并切换 Web.Config 以使用自定义模块而不是标准模块。 / p>




模块



The only changes required were to create the new module, and switch the Web.Config to use the custom module instead of the standard one.

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)
        }
    }
}






Web配置更改



所需要做的就是更改 Elmah.ErrorLogModule,Elmah 的两次出现到您自己的模块,在这种情况下为 Test.ErrorMailModule


Web Config Changes

All that's required is to change the two occurrences of Elmah.ErrorLogModule, Elmah to your own module, in this case Test.ErrorMailModule.

因此,代替这个...

So, instead of this...

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

...您现在应该拥有这个:

...you should now have this:

<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仍然负责创建电子邮件。我的看起来像这样:

You will still need the errorMail section, as Elmah is still responsible for creating the email. Mine looks like this:

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

这篇关于ELMAH-从Azure应用程序设置获取SMTP凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆