zoukankan      html  css  js  c++  java
  • 在VS2010中配制Elmah邮件发送到Gmail

               Elmah是一个开源的组件,用来记录web应用程序的error。如果你还不了解它,请先去官方网站。 它通用对Http Modules和 Http Handlers编程实现的。
    这里我们配制它自动发mail到你的Gmail邮箱。
              首先,我们在VS2010中安装NuGet,如下图:

    image1

              然后在项目Reference上点击右键,有如下Menu:

    image2

    查找Elmah项目,然后Install

    image3

    你还可以使用PowerShell按制台来Install,这样打开:

    image7

    然后在命令行输入:

    Install-Package elmah

    将Output:

    PM> Install-Package elmah
    Successfully installed 'elmah 1.1'
    Successfully added 'elmah 1.1' to MainWeb

             以上两种方式都可以,你可以任选其一。这时你可能注意要你的web.config文件已被自动修改,项目引用了elmah的程序集。

     <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>
        <httpModules>
          <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
        </httpModules>
        <httpHandlers>
          <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
        </httpHandlers>

           这时让我们来配制使用Gmail,增加配制节如下:

      <elmah>
        <security allowRemoteAccess="0" />
        <errorMail from="yourname@gmail.com" 
                   to="yourname@gmail.com" 
                   subject="error log test" 
                   priority="High"
                   async="false"
                   smtpPort="587" 
                   smtpServer="smtp.gmail.com"
                   useSsl="true" 
                   noYsod="false"
                   userName="yourname" 
                   password="yourpwd" />
      </elmah>

           Gmail使用587的端口,使用ssl。然后你还需要修改这个配制在web.config中。

        <httpModules>
          <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
          <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
        </httpModules>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
          <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
        </modules>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
        </handlers>
      </system.webServer>

           好了,这时让我们来做个测试,在任意Page中,然后运动这个Page

       1:          protected void Page_Load(object sender, EventArgs e)
       2:          {
       3:              throw new ArgumentException("Test");
       4:          }

          这时你去查看你的Gmail邮箱,就有这样的邮件了:

    gmailsnap1

          希望对您开发有帮助。


    作者:Petter Liu
    出处:http://www.cnblogs.com/wintersun/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    该文章也同时发布在我的独立博客中-Petter Liu Blog

  • 相关阅读:
    Oracle 删表前验证表名是否存在并且删除
    Mysql的建表规范与注意事项
    MYSQL总结之sql语句大全
    主机屋云服务器(绑定域名)初探
    (十)Thymeleaf用法——Themeleaf内联
    (九)Thymeleaf用法——Themeleaf注释
    (八)Thymeleaf的 th:* 属性之—— 模板布局& th:with& 属性优先级
    (七)Thymeleaf的 th:* 属性之—— th: ->设值& 遍历迭代& 条件判断
    (六)Thymeleaf的 th:* 属性之—— th: ->text& utext& href
    (五)Thymeleaf标准表达式之——[7->8]条件表达式& 默认表达式
  • 原文地址:https://www.cnblogs.com/wintersun/p/1978472.html
Copyright © 2011-2022 走看看