• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#通过电子邮件发送错误日志

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

在web.config中进行设置

1 <appSettings>
2 <add key="MailServer" value="smtp.qq.com"/>
3 <add key="MailUsername" value="[email protected]"/>
4 <add key="MailPassword" value="****"/>
5 <add key="MailFrom" value="[email protected]"/>
6 <add key="EnableErrorLogEmail" value="true"/>
7 <add key="ErrorLogEmail" value="[email protected]"/>
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中调用发送错误日志的方法,将错误日志通过邮件发送到管理员邮箱([email protected]),该方法在类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 = "[email protected]";
string _from = "[email protected]";
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 ("[email protected]"));
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("[email protected]", "******");
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("[email protected]");
10 mm.To.Add(new MailAddress("[email protected]"));
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("[email protected]", "******");
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







鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用c#生成Identicon图片发布时间:2022-07-13
下一篇:
C#怎么获取所有打开的窗体发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap