zoukankan      html  css  js  c++  java
  • C#通过电子邮件发送错误日志

    使用腾讯的邮箱:QQ邮箱默认的SMTP服务是关闭的,要自己去开通

    在web.config中进行设置

    1 <appSettings>
    2 <add key="MailServer" value="smtp.qq.com"/>
    3 <add key="MailUsername" value="41258251@qq.com"/>
    4 <add key="MailPassword" value="****"/>
    5 <add key="MailFrom" value="41258251@qq.com"/>
    6 <add key="EnableErrorLogEmail" value="true"/>
    7 <add key="ErrorLogEmail" value="zhouxq@126.com"/>
    8
    9 </appSettings>

    然后写了一个类来取这些数据:BalloonShopConfiguration.cs

     1 public static class BalloonShopConfiguration
    2 {
    3
    4 //缓存链接字符串
    5 private static string dbConnectionString;
    6 //缓存数据提供器名称
    7 private static string dbProviderName;
    8 static BalloonShopConfiguration()
    9 {
    10 dbConnectionString = ConfigurationManager.ConnectionStrings["connectionStrings"].ConnectionString;
    11 dbProviderName = ConfigurationManager.ConnectionStrings["connectionStrings"].ProviderName;
    12
    13 }
    14 //返回针对connectionStrings的数据库链接字符串
    15 public static string DbConnectionString{
    16 get
    17 {
    18 return dbConnectionString;
    19 }
    20 }
    21 //返回数据提供器名称
    22 public static string DbProviderName {
    23 get {
    24 return dbProviderName;
    25 }
    26 }
    27 //返回邮件服务器地址
    28 public static string MailServer {
    29 get {
    30 return ConfigurationManager.AppSettings["MailServer"];
    31 }
    32 }
    33 //返回电子邮件用户名
    34 public static string MailUsername {
    35 get {
    36 return ConfigurationManager.AppSettings["MailUsername"];
    37 }
    38 }
    39 //返回电子邮件密码
    40 public static string MailPassword {
    41 get {
    42 return ConfigurationManager.AppSettings["MailPassword"];
    43 }
    44 }
    45 //返回电子邮件发件人
    46 public static string MailFrom
    47 {
    48 get
    49 {
    50 return ConfigurationManager.AppSettings["MailFrom"];
    51 }
    52 }
    53 //是否发送错误日志
    54 public static bool EnableErrorLogEmail
    55 {
    56 get {
    57 return bool.Parse(ConfigurationManager.AppSettings["EnableErrorLogEmail"]);
    58 }
    59 }
    60 //返回发送错误日志的邮箱
    61 public static string ErrorLogEmail
    62 {
    63 get {
    64 return ConfigurationManager.AppSettings["ErrorLogEmail"];
    65 }
    66 }
    67 }

    通用的发送邮件的方法:SendMail

     1 public static void SendMail(string from ,string to,string title,string body) {
    2 //设置邮件客户端
    3 SmtpClient mailclient = new SmtpClient(BalloonShopConfiguration.MailServer);
    4 //设置验证信息(针对需要身份验证smtp)
    5 mailclient.Credentials = new NetworkCredential(BalloonShopConfiguration.MailFrom, BalloonShopConfiguration.MailPassword);
    6 //创建电子邮件
    7 MailMessage message = new MailMessage(from, to, title, body);
    8
    9 //发送邮件
    10 mailclient.Send(message);
    11 }

    在发生错误时,try{}catch{},在catch中调用发送错误日志的方法,将错误日志通过邮件发送到管理员邮箱(zhouxq@126.com),该方法在类Utilities中

     1 public static void ErrorLog(Exception ex) { 
    2 //获取当前时间
    3 string datetime = DateTime.Now.ToLongDateString() + ",at" + DateTime.Now.ToShortTimeString();
    4 //存储错误信息
    5 string ErrorMessage = "错误发生在" + datetime;
    6 //获取错误发生的页面
    7 System.Web.HttpContext context = HttpContext.Current;
    8 ErrorMessage += "\n\n 发生错误时请求的原始URL:" + context.Request.RawUrl;
    9 //构建错误消息
    10 ErrorMessage += "\n\n 错误消息内容:"+ex.Message;
    11 ErrorMessage +="\n\n 错误发生的对象:"+ex.Source;
    12 ErrorMessage +="\n\n 错误发生的方法:"+ex.TargetSite;
    13 ErrorMessage += "\n\n 错误发生时调用堆栈上直接帧的字符串表示形式" + ex.StackTrace;
    14
    15 if(BalloonShopConfiguration.EnableErrorLogEmail){
    16 string from = BalloonShopConfiguration.MailFrom;
    17 string to = BalloonShopConfiguration.ErrorLogEmail;
    18 string title = "BalloonShop has happed Error";
    19 string body = ErrorMessage;
    20 SendMail(from, to, title, body);
    21 }
    22 }

    在web.config中<system.web>设置:

    1 <!--发生错误时会跳到自定义页面,mode="on"时,发送错误时本机也显示oops.aspx页面,
    2 如果mode为:RemoteOnly,在远程访问机器上访问时显示Oops.aspx页面,在本机显示错误详细信息-->
    3 <customErrors mode="RemoteOnly" defaultRedirect="OoPs.aspx"></customErrors>



    其他人写的方法:

    代码:

    1  163邮箱 HOST:smtp.163.com

    public static string CreateTimeoutTestMessage(string server)
    {
    string Success = "发送成功";
    try
    {
    string _to = "1035092449@qq.com";
    string _from = "young-20@163.com";
    string _subject = "Using the new SMTP client.";
    string _body = @"Using this new feature, you can send an e-mail message from an application very easily.";
    MailMessage message = new MailMessage();
    message.From = new MailAddress(_from);
    //可以利用MailMessage.To.Add方法增加要发送的邮件地址
    message .To .Add (new MailAddress ("652105072@qq.com"));
    message.To.Add(new MailAddress(_to));
    message.Subject = _subject;
    message.Body = _body;

    //添加附件
    Attachment a = new Attachment(@"C:/Users/Administrator/Desktop/smtpclient.rar");
    message.Attachments.Add(a);
    //设置邮箱的地址或IP
    SmtpClient client = new SmtpClient(server);
    //设置邮箱端口,pop3端口:110, smtp端口是:25
    client.Port = 25;

    //设置超时时间
    client.Timeout = 9999;

    //要输入邮箱用户名与密码

    client.Credentials = new NetworkCredential("young-20@163.com", "******");
    client.Send(message);
    }
    catch (Exception ex)
    {
    Success = ex.ToString();
    }
    return Success;
    }

    2 QQ邮箱

      QQ邮箱默认的SMTP服务是关闭的,要自己去开通。

      HOST:smtp.qq.com

     1 try
    2 {
    3
    4 SmtpClient client = new SmtpClient();
    5 client.Host = "smtp.qq.com";
    6 MailMessage mm = new MailMessage();
    7 client.Port = 25;
    8
    9 mm.From = new MailAddress("652105072@qq.com");
    10 mm.To.Add(new MailAddress("1035092449@qq.com"));
    11 mm.Subject = "Hello~!";
    12 mm.Body = "HIthere.here is a post ";
    13 mm.IsBodyHtml = false;
    14 mm.Priority = MailPriority.High;
    15
    16 client.Credentials = new NetworkCredential("652105072@qq.com", "******");
    17 client .Send (mm);
    18
    19
    20 }
    21 catch (Exception ex)
    22 {
    23 MessageBox.Show(ex.Message);
    24 }


    链接地址:http://blog.csdn.net/highplayer/article/details/6007656






  • 相关阅读:
    【转】MyEclipse项目的字符编码设置
    【转】VS2008中 ATL CLR MFC Win32 区别
    【转】字符数组与字符指针
    【转】How to: Convert Between Various String Types
    【转】ATL基础BSTR CComBSTR SysAllocString
    【转】PHP ini_set ini_get 可操作配置参数列表
    【转】字符、字符数组、char、string的区别分析
    【转】PHP 计算页面执行时间
    【转】Java 如何判断String为空?
    【转】Converting char array to BSTR
  • 原文地址:https://www.cnblogs.com/zhouxiuquan/p/2321614.html
Copyright © 2011-2022 走看看