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

Sal*_*pta 2 c# asp.net

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

我正在查看 C# 或 VB.net 中的一些内容,我可以在其中将 Microsoft Enterprise Library 5.0 用于我的应用程序。

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

Sal*_*pta 5

我还可以通过微软企业库来完成我的任务

**脚步

  1. 将 Microsoft Enterprise Library 5.0 dll 添加到您的 bin 文件夹和 web.config 文件下方 **

使用代码进行测试

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);
        }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)