-
Notifications
You must be signed in to change notification settings - Fork 467
/
DefaultSmtpSender.cs
296 lines (264 loc) · 7.73 KB
/
DefaultSmtpSender.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2004-2017 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if FEATURE_SMTP
#pragma warning disable 618 // Mono marked SmtpClient obsolete
namespace Castle.Core.Smtp
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Net.Mail;
#if DOTNET40
using System.Security;
#endif
using System.Security.Permissions;
using Castle.Core.Internal;
/// <summary>
/// Default <see cref="IEmailSender"/> implementation.
/// </summary>
public class DefaultSmtpSender : IEmailSender
{
private bool asyncSend;
private readonly string hostname;
private int port = 25;
private int? timeout;
private bool useSsl;
private readonly NetworkCredential credentials = new NetworkCredential();
/// <summary>
/// Initializes a new instance of the <see cref="DefaultSmtpSender"/> class based on the <see cref="SmtpClient"/> configuration provided in the application configuration file.
/// </summary>
/// <remarks>
/// This constructor is based on the default <see cref="SmtpClient"/> configuration in the application configuration file.
/// </remarks>
public DefaultSmtpSender() { }
/// <summary>
/// This service implementation
/// requires a host name in order to work
/// </summary>
/// <param name="hostname">The smtp server name</param>
public DefaultSmtpSender(string hostname)
{
this.hostname = hostname;
}
/// <summary>
/// Gets or sets the port used to
/// access the SMTP server
/// </summary>
public int Port
{
get { return port; }
set { port = value; }
}
/// <summary>
/// Gets the hostname.
/// </summary>
/// <value>The hostname.</value>
public string Hostname
{
get { return hostname; }
}
/// <summary>
/// Gets or sets a value which is used to
/// configure if emails are going to be sent asynchronously or not.
/// </summary>
public bool AsyncSend
{
get { return asyncSend; }
set { asyncSend = value; }
}
/// <summary>
/// Gets or sets a value that specifies
/// the amount of time after which a synchronous Send call times out.
/// </summary>
public int Timeout
{
get { return timeout.HasValue ? timeout.Value : 0; }
set { timeout = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the email should be sent using
/// a secure communication channel.
/// </summary>
/// <value><c>true</c> if should use SSL; otherwise, <c>false</c>.</value>
public bool UseSsl
{
get { return useSsl; }
set { useSsl = value; }
}
/// <summary>
/// Sends a message.
/// </summary>
/// <exception cref="ArgumentNullException">If any of the parameters is null</exception>
/// <param name="from">From field</param>
/// <param name="to">To field</param>
/// <param name="subject">e-mail's subject</param>
/// <param name="messageText">message's body</param>
#if FEATURE_SECURITY_PERMISSIONS && DOTNET40
[SecuritySafeCritical]
#endif
public void Send(String from, String to, String subject, String messageText)
{
if (from == null) throw new ArgumentNullException("from");
if (to == null) throw new ArgumentNullException("to");
if (subject == null) throw new ArgumentNullException("subject");
if (messageText == null) throw new ArgumentNullException("messageText");
Send(new MailMessage(from, to, subject, messageText));
}
/// <summary>
/// Sends a message.
/// </summary>
/// <exception cref="ArgumentNullException">If the message is null</exception>
/// <param name="message">Message instance</param>
#if FEATURE_SECURITY_PERMISSIONS && DOTNET40
[SecuritySafeCritical]
#endif
public void Send(MailMessage message)
{
InternalSend(message);
}
#if FEATURE_SECURITY_PERMISSIONS && DOTNET40
[SecurityCritical]
#endif
private void InternalSend(MailMessage message)
{
if (message == null) throw new ArgumentNullException("message");
if (asyncSend)
{
// The MailMessage must be disposed after sending the email.
// The code creates a delegate for deleting the mail and adds
// it to the smtpClient.
// After the mail is sent, the message is disposed and the
// eventHandler removed from the smtpClient.
SmtpClient smtpClient = CreateSmtpClient();
Guid msgGuid = new Guid();
SendCompletedEventHandler sceh = null;
sceh = delegate(object sender, AsyncCompletedEventArgs e)
{
if (msgGuid == (Guid)e.UserState)
message.Dispose();
// The handler itself, cannot be null, test omitted
smtpClient.SendCompleted -= sceh;
};
smtpClient.SendCompleted += sceh;
smtpClient.SendAsync(message, msgGuid);
}
else
{
using (message)
{
SmtpClient smtpClient = CreateSmtpClient();
smtpClient.Send(message);
}
}
}
#if FEATURE_SECURITY_PERMISSIONS && DOTNET40
[SecuritySafeCritical]
#endif
public void Send(IEnumerable<MailMessage> messages)
{
foreach (MailMessage message in messages)
{
Send(message);
}
}
/// <summary>
/// Gets or sets the domain.
/// </summary>
/// <value>The domain.</value>
public String Domain
{
get { return credentials.Domain; }
set { credentials.Domain = value; }
}
/// <summary>
/// Gets or sets the name of the user.
/// </summary>
/// <value>The name of the user.</value>
public String UserName
{
get { return credentials.UserName; }
set { credentials.UserName = value; }
}
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
public String Password
{
get { return credentials.Password; }
set { credentials.Password = value; }
}
/// <summary>
/// Configures the sender
/// with port information and eventual credential
/// informed
/// </summary>
/// <param name="smtpClient">Message instance</param>
#if FEATURE_SECURITY_PERMISSIONS && DOTNET40
[SecurityCritical]
#endif
protected virtual void Configure(SmtpClient smtpClient)
{
smtpClient.Credentials = null;
if (CanAccessCredentials() && HasCredentials)
{
smtpClient.Credentials = credentials;
}
if (timeout.HasValue)
{
smtpClient.Timeout = timeout.Value;
}
if (useSsl)
{
smtpClient.EnableSsl = useSsl;
}
}
/// <summary>
/// Gets a value indicating whether credentials were informed.
/// </summary>
/// <value>
/// <see langword="true"/> if this instance has credentials; otherwise, <see langword="false"/>.
/// </value>
private bool HasCredentials
{
get { return !string.IsNullOrEmpty(credentials.UserName); }
}
#if FEATURE_SECURITY_PERMISSIONS && DOTNET40
[SecuritySafeCritical]
#endif
private SmtpClient CreateSmtpClient()
{
if (string.IsNullOrEmpty(hostname))
{
// No hostname configured, use the settings provided in system.net.smtp (SmtpClient default behavior)
return new SmtpClient();
}
// A hostname is provided - init and configure using configured settings
var smtpClient = new SmtpClient(hostname, port);
Configure(smtpClient);
return smtpClient;
}
private static bool CanAccessCredentials()
{
#if FEATURE_SECURITY_PERMISSIONS
return new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).IsGranted();
#else
return false;
#endif
}
}
}
#pragma warning restore 618 // Mono marked SmtpClient obsolete
#endif