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("自定義的異常");