C#通过电子邮件发送错误日志
2012-01-13 14:44
323 查看
使用腾讯的邮箱:QQ邮箱默认的SMTP服务是关闭的,要自己去开通
在web.config中进行设置
然后写了一个类来取这些数据:BalloonShopConfiguration.cs
通用的发送邮件的方法:SendMail
在发生错误时,try{}catch{},在catch中调用发送错误日志的方法,将错误日志通过邮件发送到管理员邮箱(zhouxq@126.com),该方法在类Utilities中
在web.config中<system.web>设置:
其他人写的方法:
代码:
1 163邮箱 HOST:smtp.163.com
2 QQ邮箱
QQ邮箱默认的SMTP服务是关闭的,要自己去开通。
HOST:smtp.qq.com
链接地址:http://blog.csdn.net/highplayer/article/details/6007656
在web.config中进行设置
<appSettings> <add key="MailServer" value="smtp.qq.com"/> <add key="MailUsername" value="41258251@qq.com"/> <add key="MailPassword" value="****"/> <add key="MailFrom" value="41258251@qq.com"/> <add key="EnableErrorLogEmail" value="true"/> <add key="ErrorLogEmail" value="zhouxq@126.com"/> </appSettings>
然后写了一个类来取这些数据:BalloonShopConfiguration.cs
public static class BalloonShopConfiguration { //缓存链接字符串 private static string dbConnectionString; //缓存数据提供器名称 private static string dbProviderName; static BalloonShopConfiguration() { dbConnectionString = ConfigurationManager.ConnectionStrings["connectionStrings"].ConnectionString; dbProviderName = ConfigurationManager.ConnectionStrings["connectionStrings"].ProviderName; } //返回针对connectionStrings的数据库链接字符串 public static string DbConnectionString{ get { return dbConnectionString; } } //返回数据提供器名称 public static string DbProviderName { get { return dbProviderName; } } //返回邮件服务器地址 public static string MailServer { get { return ConfigurationManager.AppSettings["MailServer"]; } } //返回电子邮件用户名 public static string MailUsername { get { return ConfigurationManager.AppSettings["MailUsername"]; } } //返回电子邮件密码 public static string MailPassword { get { return ConfigurationManager.AppSettings["MailPassword"]; } } //返回电子邮件发件人 public static string MailFrom { get { return ConfigurationManager.AppSettings["MailFrom"]; } } //是否发送错误日志 public static bool EnableErrorLogEmail { get { return bool.Parse(ConfigurationManager.AppSettings["EnableErrorLogEmail"]); } } //返回发送错误日志的邮箱 public static string ErrorLogEmail { get { return ConfigurationManager.AppSettings["ErrorLogEmail"]; } } }
通用的发送邮件的方法:SendMail
public static void SendMail(string from ,string to,string title,string body) { //设置邮件客户端 SmtpClient mailclient = new SmtpClient(BalloonShopConfiguration.MailServer); //设置验证信息(针对需要身份验证smtp) mailclient.Credentials = new NetworkCredential(BalloonShopConfiguration.MailFrom, BalloonShopConfiguration.MailPassword); //创建电子邮件 MailMessage message = new MailMessage(from, to, title, body); //发送邮件 mailclient.Send(message); }
在发生错误时,try{}catch{},在catch中调用发送错误日志的方法,将错误日志通过邮件发送到管理员邮箱(zhouxq@126.com),该方法在类Utilities中
public static void ErrorLog(Exception ex) { //获取当前时间 string datetime = DateTime.Now.ToLongDateString() + ",at" + DateTime.Now.ToShortTimeString(); //存储错误信息 string ErrorMessage = "错误发生在" + datetime; //获取错误发生的页面 System.Web.HttpContext context = HttpContext.Current; ErrorMessage += "\n\n 发生错误时请求的原始URL:" + context.Request.RawUrl; //构建错误消息 ErrorMessage += "\n\n 错误消息内容:"+ex.Message; ErrorMessage +="\n\n 错误发生的对象:"+ex.Source; ErrorMessage +="\n\n 错误发生的方法:"+ex.TargetSite; ErrorMessage += "\n\n 错误发生时调用堆栈上直接帧的字符串表示形式" + ex.StackTrace; if(BalloonShopConfiguration.EnableErrorLogEmail){ string from = BalloonShopConfiguration.MailFrom; string to = BalloonShopConfiguration.ErrorLogEmail; string title = "BalloonShop has happed Error"; string body = ErrorMessage; SendMail(from, to, title, body); } }
在web.config中<system.web>设置:
<!--发生错误时会跳到自定义页面,mode="on"时,发送错误时本机也显示oops.aspx页面, 如果mode为:RemoteOnly,在远程访问机器上访问时显示Oops.aspx页面,在本机显示错误详细信息--> <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
try { SmtpClient client = new SmtpClient(); client.Host = "smtp.qq.com"; MailMessage mm = new MailMessage(); client.Port = 25; mm.From = new MailAddress("652105072@qq.com"); mm.To.Add(new MailAddress("1035092449@qq.com")); mm.Subject = "Hello~!"; mm.Body = "HIthere.here is a post "; mm.IsBodyHtml = false; mm.Priority = MailPriority.High; client.Credentials = new NetworkCredential("652105072@qq.com", "******"); client .Send (mm); } catch (Exception ex) { MessageBox.Show(ex.Message); }
链接地址:http://blog.csdn.net/highplayer/article/details/6007656
相关文章推荐
- 微信通过openID发送消息/后台post、get提交并接收数据 C# .NET 配置404,500等错误信息 连接字符串
- android自己获取并保存错误日志(可以通过邮件发送到自己邮箱)
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- C#线程类框架,支持消息队列,通过postmessage从UI线程发送消息到工作者线程
- 使用asp.net通过gmail发送电子邮件
- C# 发送电子邮件
- 从apache的错误日志中找出恶意ip,并通过iptables过滤
- MOSS中图形化通过记录日志的方式来进行查看“发生意外错误”的原因WSP下载
- C# 发送电子邮件(含附件)用到的类 system.Net.mail
- C# 怎样通过ip和端口向一台设备发送指令
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- 通过C#发送自定义的html格式邮件
- 用C#通过Nokia手机发送短信
- log4j2通过tcp发送json日志到logstash Elasticsearch
- VS2010编写C#代码时..只要想选中一排代码.想复制粘贴..就会出错..然后问我发不发送错误信息..然后就又重启
- 一个发送电子邮件的类(C#&asp.net2)
- C# 通过扩展WebBrowser捕获网络连接错误信息
- PHP 错误与异常 笔记与总结(7)将错误日志以邮件方式发送
- C#.NET使用HTML模板发送电子邮件
- .net C# web程序运行中错误日志写入文本文件中