我們如何使用Microsoft Enterprise Library 5.0在asp.net中發送電子郵件?我們如何使用Microsoft企業庫在asp.net中發送電子郵件?
我正在看C#或VB.net中的一些東西,我可以在其中使用Microsoft Enterprise Library 5.0作爲我的應用程序。
如果開發過程中出現代碼異常,我想發郵件給開發者。
我們如何使用Microsoft Enterprise Library 5.0在asp.net中發送電子郵件?我們如何使用Microsoft企業庫在asp.net中發送電子郵件?
我正在看C#或VB.net中的一些東西,我可以在其中使用Microsoft Enterprise Library 5.0作爲我的應用程序。
如果開發過程中出現代碼異常,我想發郵件給開發者。
嘿,夥計們,現在我還可以通過微軟的企業庫,以實現我的任務
**步驟 1)加入微軟企業庫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);
}
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="[email protected]" fromAddress="[email protected]" subjectLineStarter="Exception Occured" smtpServer="smtp.gmail.com" smtpPort="587"
formatter="Text Formatter" authenticationMode="UserNameAndPassword" useSSL="true" userName="[email protected]" 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}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
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 & 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>
在此博客文章中有一個使用日誌記錄應用程序塊將錯誤記錄到電子郵件的示例。
我必須弄清楚實現我的任務記錄錯誤的電子郵件解決方案。我使用了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="[email protected]" to="[email protected]" subject="Error" smtpServer="smtp.gmail.com" smtpPort="587" userName="[email protected]" 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>