-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailerrorsenderlan.cs
69 lines (61 loc) · 2.16 KB
/
emailerrorsenderlan.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public static class StringExtensions
{
public static string Repeat(this string input, int count)
{
if (input == null)
{
return null;
}
var sb = new StringBuilder();
for (var repeat = 0; repeat < count; repeat++)
{
sb.Append(input);
}
return sb.ToString();
}
}
class EmailErrorSender
{
private readonly string _exchangeserver;
private readonly string _appName;
private readonly string _mailto;
private readonly string _mailfrom;
public EmailErrorSender(string exchangeserver, string appName,string mailto,string mailfrom)
{
_exchangeserver = exchangeserver;
_appName = appName;
_mailto = mailto;
_mailfrom = mailfrom;
}
public void Write(Exception ex)
{
var smtpClient = new SmtpClient();
var message = new MailMessage();
MailAddress fromAddress = new MailAddress(_mailfrom);
smtpClient.Host = _exchangeserver;
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;
message.From = fromAddress;
message.To.Add(_mailto);
var body = getException(ex);
message.Body = body;
message.Subject =string.Format("{0} error", _appName);
message.IsBodyHtml = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
}
string getException(Exception ex,int indent=0)
{
string body = string.Empty;
string ind = "\t".Repeat(indent);
body += string.Format(ind+"ex.message = {0}\n", ex.Message);
body += string.Format(ind+"ex.stacktrace = {0}\n", ex.StackTrace);
body += string.Format(ind+"ex.source = {0}\n", ex.Message);
if (ex.InnerException != null)
{
body += "inner exception:\n";
body += getException(ex,indent+1);
}
return body;
}
}