C# 如何在asp.net中使用Microsoft企业库发送电子邮件?

C# 如何在asp.net中使用Microsoft企业库发送电子邮件?,c#,asp.net,C#,Asp.net,我们如何使用Microsoft Enterprise Library 5.0在asp.net中发送电子邮件 我正在研究C#或VB.net中的一些东西,在那里我可以将Microsoft企业图书馆5.0用于我的应用程序 如果在开发过程中代码中出现任何异常,我想向开发人员发送电子邮件。这里有一个使用日志应用程序块将错误记录到这篇博文的电子邮件中的示例 在这篇博文中有一个使用日志应用程序块将错误记录到电子邮件的示例 我已经找到了解决方案,可以将我的任务日志错误记录为电子邮件。我使用过ELMAH:错误日志

我们如何使用Microsoft Enterprise Library 5.0在asp.net中发送电子邮件

我正在研究C#或VB.net中的一些东西,在那里我可以将Microsoft企业图书馆5.0用于我的应用程序


如果在开发过程中代码中出现任何异常,我想向开发人员发送电子邮件。

这里有一个使用日志应用程序块将错误记录到这篇博文的电子邮件中的示例


在这篇博文中有一个使用日志应用程序块将错误记录到电子邮件的示例


我已经找到了解决方案,可以将我的任务日志错误记录为电子邮件。我使用过ELMAH:错误日志模块和处理程序。

配置步骤 1) 下载Elmah.dll,将Elmah.dll复制到您的bin文件夹中 2) 使用下面的web.config文件。就这样

当您的代码出现任何异常时,将向受尊敬的人发送电子邮件

Web.Config

<?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"/>
    <errorMail from="xyz@gmail.com" to="abc@example.com" subject="Error" smtpServer="smtp.gmail.com" smtpPort="587" userName="xyz@gmail.com" password="***********" useSsl="true" async="true"/>
  </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"/>
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
    </httpModules>
    <compilation debug="true"/>
  </system.web>
</configuration>
try
        {
            
            throw new NullReferenceException("General null reference");
            
        }
        catch (Exception ex)
        {
            LogEntry logEntry = new LogEntry();
            logEntry.EventId = 100;
            logEntry.Priority = 2;
            logEntry.Message = "Informational message";
            Logger.Write(ex.ToString() + logEntry);
        }

我已经找到了解决方案,可以将我的任务日志错误记录为电子邮件。我使用过ELMAH:错误日志模块和处理程序。

配置步骤 1) 下载Elmah.dll,将Elmah.dll复制到您的bin文件夹中 2) 使用下面的web.config文件。就这样

当您的代码出现任何异常时,将向受尊敬的人发送电子邮件

Web.Config

<?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"/>
    <errorMail from="xyz@gmail.com" to="abc@example.com" subject="Error" smtpServer="smtp.gmail.com" smtpPort="587" userName="xyz@gmail.com" password="***********" useSsl="true" async="true"/>
  </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"/>
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
    </httpModules>
    <compilation debug="true"/>
  </system.web>
</configuration>
try
        {
            
            throw new NullReferenceException("General null reference");
            
        }
        catch (Exception ex)
        {
            LogEntry logEntry = new LogEntry();
            logEntry.EventId = 100;
            logEntry.Priority = 2;
            logEntry.Message = "Informational message";
            Logger.Write(ex.ToString() + logEntry);
        }

我还可以通过microsoft企业库完成我的任务

**台阶

  • 将microsoft enterprise library 5.0 dll添加到您的bin文件夹中,然后在web.config文件下方添加 **
  • 使用代码进行测试

    <?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"/>
        <errorMail from="xyz@gmail.com" to="abc@example.com" subject="Error" smtpServer="smtp.gmail.com" smtpPort="587" userName="xyz@gmail.com" password="***********" useSsl="true" async="true"/>
      </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"/>
          <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
        </httpModules>
        <compilation debug="true"/>
      </system.web>
    </configuration>
    
    try
            {
                
                throw new NullReferenceException("General null reference");
                
            }
            catch (Exception ex)
            {
                LogEntry logEntry = new LogEntry();
                logEntry.EventId = 100;
                logEntry.Priority = 2;
                logEntry.Message = "Informational message";
                Logger.Write(ex.ToString() + logEntry);
            }
    
    Web.config文件

    <configuration>
      <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
      </configSections>
      <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
          <add name="Email Trace Listener"
               type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               toAddress="xyz@gmail.com" fromAddress="abc@gmail.com" subjectLineStarter="Exception Occured" smtpServer="smtp.gmail.com" smtpPort="587"
               formatter="Text Formatter" authenticationMode="UserNameAndPassword" useSSL="true" userName="abc@gmail.com" password="********"/>
          <add name="Flat File Trace Listener"
               type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               fileName="trace.log"/>
        </listeners>
        <formatters>
          <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}" name="Text Formatter"/>
        </formatters>
        <categorySources>
          <add switchValue="All" name="General">
            <listeners>
              <add name="Flat File Trace Listener"/>
              <add name="Email Trace Listener"/>
            </listeners>
          </add>
        </categorySources>
        <specialSources>
          <allEvents switchValue="All" name="All Events"/>
          <notProcessed switchValue="All" name="Unprocessed Category"/>
          <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
              <add name="Flat File Trace Listener"/>
              <add name="Email Trace Listener"/>
            </listeners>
          </errors>
        </specialSources>
      </loggingConfiguration>
      <system.web>
        <compilation debug="true"/>
      </system.web>
    </configuration>
    

    我还可以通过microsoft企业库完成我的任务

    **台阶

  • 将microsoft enterprise library 5.0 dll添加到您的bin文件夹中,然后在web.config文件下方添加 **
  • 使用代码进行测试

    <?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"/>
        <errorMail from="xyz@gmail.com" to="abc@example.com" subject="Error" smtpServer="smtp.gmail.com" smtpPort="587" userName="xyz@gmail.com" password="***********" useSsl="true" async="true"/>
      </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"/>
          <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
        </httpModules>
        <compilation debug="true"/>
      </system.web>
    </configuration>
    
    try
            {
                
                throw new NullReferenceException("General null reference");
                
            }
            catch (Exception ex)
            {
                LogEntry logEntry = new LogEntry();
                logEntry.EventId = 100;
                logEntry.Priority = 2;
                logEntry.Message = "Informational message";
                Logger.Write(ex.ToString() + logEntry);
            }
    
    Web.config文件

    <configuration>
      <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
      </configSections>
      <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
          <add name="Email Trace Listener"
               type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               toAddress="xyz@gmail.com" fromAddress="abc@gmail.com" subjectLineStarter="Exception Occured" smtpServer="smtp.gmail.com" smtpPort="587"
               formatter="Text Formatter" authenticationMode="UserNameAndPassword" useSSL="true" userName="abc@gmail.com" password="********"/>
          <add name="Flat File Trace Listener"
               type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               fileName="trace.log"/>
        </listeners>
        <formatters>
          <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
               template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}" name="Text Formatter"/>
        </formatters>
        <categorySources>
          <add switchValue="All" name="General">
            <listeners>
              <add name="Flat File Trace Listener"/>
              <add name="Email Trace Listener"/>
            </listeners>
          </add>
        </categorySources>
        <specialSources>
          <allEvents switchValue="All" name="All Events"/>
          <notProcessed switchValue="All" name="Unprocessed Category"/>
          <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
              <add name="Flat File Trace Listener"/>
              <add name="Email Trace Listener"/>
            </listeners>
          </errors>
        </specialSources>
      </loggingConfiguration>
      <system.web>
        <compilation debug="true"/>
      </system.web>
    </configuration>