ASP.NET发送email,默认是25端口
/// <summary>发送email,默认是25端口 /// /// </summary> /// <param name="title">邮件标题</param> /// <param name="body">邮件内容</param> /// <param name="toAdress">收件人</param> /// <param name="fromAdress">发件人</param> /// <param name="userName">发件用户名</param> /// <param name="userPwd">发件密码</param> /// <param name="smtpHost">smtp地址</param> public static void SendMail(string title, string body, string toAdress, string fromAdress, string userName, string userPwd, string smtpHost) { try { MailAddress to = new MailAddress(toAdress); MailAddress from = new MailAddress(fromAdress); System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to); message.IsBodyHtml = true; // 如果不加上这句那发送的邮件内容中有HTML会原样输出 message.Subject = title; message.Body = body; SmtpClient smtp = new SmtpClient(); smtp.UseDefaultCredentials = true; smtp.Port = 25; smtp.Credentials = new NetworkCredential(userName, userPwd); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Host = smtpHost; message.To.Add(toAdress); smtp.Send(message); } catch (Exception ex) { throw ex; } }
.net异常处理加到Global程序文件中的
protected void Application_Error(object sender, EventArgs e) { //在出现未处理的错误时运行的代码 Exception objErr = Server.GetLastError().GetBaseException(); string time = DateTime.Now.ToString(); string error = ""; error += "<strong>异常信息: </strong>" + objErr.Message + "<br>"; error += "<strong>异常发生时间:</strong>" + time + "<br>"; error += "<strong>IP: </strong>" + Request.UserHostAddress + "<br>"; error += "<strong>发生异常页: </strong>" + Request.Url.ToString() + "<br>"; string url_re = ""; if (Request.UrlReferrer != null) { url_re = Request.UrlReferrer.ToString(); } error += "<strong>上次请求的URL: </strong>" + url_re + "<br>"; error += "<strong>堆栈跟踪: </strong><br>" + objErr.StackTrace.Replace("\r\n", "<br>") + "<br>"; if (!objErr.Message.Contains("不存在")) { Xiaobin.Utility.Tool.SendMail("网站出现异常" + time, error, "35754311@qq.com", "181796286@qq.com", "181796286", "123456", "smtp.qq.com"); } }
设置web.config文件
//On打开错误机制 默认指向Error.html 404错误指向FileNotFound.html
加在system.web节点中
<customErrors mode="On" defaultRedirect="Error.html">
<error statusCode="404" redirect="FileNotFound.html"/>
</customErrors>
创建test.aspx测试一下异常处理
throw new Exception("自定义的异常");