Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created February 15, 2015 12:38
Show Gist options
  • Save KentaYamada/63bdd80ccd57a31fdfad to your computer and use it in GitHub Desktop.
Save KentaYamada/63bdd80ccd57a31fdfad to your computer and use it in GitHub Desktop.
エラーログを送信する
using System;
using System.Net;
using System.Net.Mail;
using System.Diagnostics;
using System.Text;
public static class SendErrorLog
{
public static void Send(Exception ex)
{
string fromAddress = "from address here";
string toAddress = "to address here";
string title = "mail title here";
var sb = new StringBuilder();
sb.AppendFormat("発生時刻:{0}\n", DateTime.Now.ToString("yyyy/MM/dd hh:mm"));
sb.AppendFormat("発生箇所:{0}\n", ex.TargetSite);
sb.AppendFormat("内容:{0}\n", ex.Message);
sb.AppendFormat("スタックトレース:{0}\n", ex.StackTrace);
using (var smtp = new SmtpClient())
{
smtp.Host = "host name here";
smtp.Port = 999; //port no here
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("username here", "password here"); //認証設定
smtp.EnableSsl = true;
try
{
smtp.Send(address, address, title, sb.ToString());
Debug.Print("Success.");
}
catch
{
Debug.Print("Mail send failure.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment