加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何在ELMAH中连接自定义电子邮件提供商?

发布时间:2020-12-16 09:54:58 所属栏目:asp.Net 来源:网络整理
导读:我已经在谷歌小组的elmah小组上问了这个,但我仍然感到神秘,所以我想我会把它扔到一个更大的游泳池.这是我目前的代码的原始注释. 我在我的MVC 5应用程序上设置了ELMAH,我想使用自定义电子邮件服务来发送电子邮件,而不是使用默认设置的smtp发送. (这是组织的偏
我已经在谷歌小组的elmah小组上问了这个,但我仍然感到神秘,所以我想我会把它扔到一个更大的游泳池.这是我目前的代码的原始注释.

我在我的MVC 5应用程序上设置了ELMAH,我想使用自定义电子邮件服务来发送电子邮件,而不是使用默认设置的smtp发送. (这是组织的偏好.)我一直在探索它,但无法弄清楚如何做到这一点.我的服务调用工作得很好,但是我无法将其挂起以便在出现异常时运行.任何帮助将不胜感激.

我设置的是以下内容:

Web.config(删除了非elmah相关的行):

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application,please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler,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>
    ...
    <add key="elmah.mvc.disableHandler" value="false" />
    <add key="elmah.mvc.disableHandleErrorFilter" value="false" />
    <add key="elmah.mvc.requiresAuthentication" value="false" />
    <add key="elmah.mvc.IgnoreDefaultRoute" value="false" />
    <add key="elmah.mvc.allowedRoles" value="*" />
    <add key="elmah.mvc.allowedUsers" value="*" />
    <add key="elmah.mvc.route" value="elmah" />
</appSettings>
  <connectionStrings>
    <add name="BFRDPDB" providerName="System.Data.SqlClient" connectionString="blahblah;" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <customErrors mode="Off" defaultRedirect="~/Home/Error">
      <error statusCode="404" redirect="~/Home/Error" />
    </customErrors>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule,Elmah" />
      <add name="ErrorMail" type="BFRDP.Infrastructure.ErrorMailModule" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule,Elmah" />
    </httpModules>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule,Elmah" preCondition="managedHandler" />
      <add name="ErrorMail" type="BFRDP.Infrastructure.ErrorMailModule" preCondition="managedHandler" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule,Elmah" preCondition="managedHandler" />
    </modules>
  </system.webServer>
  <elmah>
    <security allowRemoteAccess="false" />
    <errorLog type="Elmah.SqlErrorLog,Elmah"
              connectionStringName="BFRDPDB"
              applicationName="FarmAnswers.org" /> 
  </elmah>
  <location path="elmah">
    <system.web>
      <httpHandlers>
        <add verb="POST,GET,HEAD"
             path="elmah.axd"
             type="Elmah.ErrorLogPageFactory,Elmah" />
      </httpHandlers>
      <authorization>
        <allow roles="SuperAdmin" />
        <deny users="*" />
      </authorization>
    </system.web>
    <system.webServer>
      <handlers>
        <add name="ELMAH"
             verb="POST,HEAD"
             path="elmah"
             type="Elmah.ErrorLogPageFactory,Elmah"
             preCondition="integratedMode" />
      </handlers>
    </system.webServer>
  </location>
</configuration>

我的ErrorMailModule.cs文件如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace BFRDP.Infrastructure
{/// <summary>
    /// Summary description for ErrorMailModule
    /// </summary>
    public class ErrorMailModule : Elmah.ErrorMailModule
    {
        public ErrorMailModule()
        {
        }



        protected override void SendMail(MailMessage mail)
        {
            if (mail == null)
                throw new ArgumentNullException("mail");
            // Code to send email here through internal provider
        }

    }
}

我的错误确实记录在数据库中,但从未到达发送电子邮件的代码.

我究竟做错了什么?

解决方法

在我的web.config部分添加了errorMail设置,应用程序可以访问我的自定义模块:

<elmah>
    <security allowRemoteAccess="false" />
    <errorLog type="Elmah.SqlErrorLog,Elmah" connectionStringName="BFRDPDB" applicationName="FarmAnswers.org" />
    <errorMail
    from="elmah@example.com"
    to="admin@example.com"
    subject="..."
    priority="Low"
    async="true"
    smtpPort="25"
    smtpServer="smtp.example.com"
    useSsl="true"
    userName="johndoe"
    password="secret"
    noYsod="true" />
  </elmah>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读