-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailSender.cs
More file actions
131 lines (119 loc) · 4.53 KB
/
EmailSender.cs
File metadata and controls
131 lines (119 loc) · 4.53 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Microsoft.SqlServer.Server;
using System;
using System.Collections;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
namespace EmailSenderLibrary
{
public class EmailSender
{
private class EmailSendResult
{
public bool Success { get; set; }
public string Message { get; set; }
public long TimingMs { get; set; }
public string ExceptionType { get; set; }
}
[SqlFunction(
DataAccess = DataAccessKind.None,
FillRowMethodName = "FillRow",
TableDefinition = "success BIT, message NVARCHAR(MAX), timingMs BIGINT, exceptionType NVARCHAR(200)"
)]
public static IEnumerable FrisiaSendMail(
SqlString smtpHost,
SqlInt32 smtpPort,
SqlString smtpUser,
SqlString smtpPass,
SqlString from,
SqlString to,
SqlString subject,
SqlString body,
SqlBoolean enableSsl,
SqlInt32 timeout
)
{
var result = new EmailSendResult();
var stopwatch = Stopwatch.StartNew();
try
{
ValidateParams(smtpHost, from, to, subject, body);
using (var client = new SmtpClient(smtpHost.Value, smtpPort.IsNull ? 25 : smtpPort.Value))
{
client.EnableSsl = enableSsl.IsNull ? false : enableSsl.Value;
if (!timeout.IsNull && timeout.Value > 0)
client.Timeout = timeout.Value;
if (!smtpUser.IsNull)
{
string pass = smtpPass.IsNull ? "" : smtpPass.Value;
client.Credentials = new NetworkCredential(smtpUser.Value, pass);
}
using (var mail = new MailMessage())
{
mail.From = new MailAddress(from.Value);
foreach (var address in to.Value
.Split(new[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries))
{
var trimmed = address.Trim();
if (!string.IsNullOrEmpty(trimmed))
{
mail.To.Add(trimmed);
}
}
mail.Subject = subject.Value;
mail.Body = body.Value;
mail.IsBodyHtml = true;
client.Send(mail);
result.Success = true;
result.Message = "Email sent successfully.";
}
}
}
catch (SmtpFailedRecipientsException ex)
{
result.Success = false;
result.Message = "Error: Failed to deliver to one or more recipients. " + ex.Message;
result.ExceptionType = nameof(SmtpFailedRecipientsException);
}
catch (SmtpException ex)
{
result.Success = false;
result.Message = "SMTP Error: " + ex.Message;
result.ExceptionType = nameof(SmtpException);
}
catch (Exception ex)
{
result.Success = false;
result.Message = "Unexpected error when sending email: " + ex.Message;
result.ExceptionType = ex.GetType().Name;
}
finally
{
stopwatch.Stop();
result.TimingMs = stopwatch.ElapsedMilliseconds;
}
yield return result;
}
public static void FillRow(
object obj,
out SqlBoolean success,
out SqlString message,
out SqlInt64 timing,
out SqlString exceptionType
)
{
var result = (EmailSendResult)obj;
success = result.Success;
message = result.Message ?? SqlString.Null;
timing = result.TimingMs;
exceptionType = result.ExceptionType ?? SqlString.Null;
}
private static void ValidateParams(
SqlString smtpHost, SqlString from, SqlString to, SqlString subject, SqlString body)
{
if (smtpHost.IsNull || from.IsNull || to.IsNull || subject.IsNull || body.IsNull)
throw new ArgumentNullException("smtpHost, from, to, subject, body are mandatory.");
}
}
}