您的位置:首页 > 编程语言 > C#

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

2012-01-13 14:44 323 查看
使用腾讯的邮箱:QQ邮箱默认的SMTP服务是关闭的,要自己去开通

在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐