/
SMTP_Server.cs
273 lines (227 loc) · 9.95 KB
/
SMTP_Server.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
using System;
using System.Collections.Generic;
using System.Text;
using LumiSoft.Net.TCP;
using LumiSoft.Net.AUTH;
namespace LumiSoft.Net.SMTP.Server
{
/// <summary>
/// This class implements SMTP server. Defined RFC 5321.
/// </summary>
public class SMTP_Server : TCP_Server<SMTP_Session>
{
private List<string> m_pServiceExtentions = null;
private string m_GreetingText = "";
private int m_MaxBadCommands = 30;
private int m_MaxTransactions = 10;
private int m_MaxMessageSize = 10000000;
private int m_MaxRecipients = 100;
/// <summary>
/// Default constructor.
/// </summary>
public SMTP_Server()
{
m_pServiceExtentions = new List<string>();
m_pServiceExtentions.Add(SMTP_ServiceExtensions.PIPELINING);
m_pServiceExtentions.Add(SMTP_ServiceExtensions.SIZE);
m_pServiceExtentions.Add(SMTP_ServiceExtensions.STARTTLS);
m_pServiceExtentions.Add(SMTP_ServiceExtensions._8BITMIME);
m_pServiceExtentions.Add(SMTP_ServiceExtensions.BINARYMIME);
m_pServiceExtentions.Add(SMTP_ServiceExtensions.CHUNKING);
}
// TODO:
//public override Dispose
#region override method OnMaxConnectionsExceeded
/// <summary>
/// Is called when new incoming session and server maximum allowed connections exceeded.
/// </summary>
/// <param name="session">Incoming session.</param>
/// <remarks>This method allows inhereted classes to report error message to connected client.
/// Session will be disconnected after this method completes.
/// </remarks>
protected override void OnMaxConnectionsExceeded(SMTP_Session session)
{
session.TcpStream.WriteLine("421 Client host rejected: too many connections, please try again later.");
}
#endregion
#region override method OnMaxConnectionsPerIPExceeded
/// <summary>
/// Is called when new incoming session and server maximum allowed connections per connected IP exceeded.
/// </summary>
/// <param name="session">Incoming session.</param>
/// <remarks>This method allows inhereted classes to report error message to connected client.
/// Session will be disconnected after this method completes.
/// </remarks>
protected override void OnMaxConnectionsPerIPExceeded(SMTP_Session session)
{
session.TcpStream.WriteLine("421 Client host rejected: too many connections from your IP(" + session.RemoteEndPoint.Address + "), please try again later.");
}
#endregion
#region Properties implementation
/// <summary>
/// Gets or sets SMTP server supported service extentions.
/// Supported values: PIPELINING,SIZE,STARTTLS,8BITMIME,BINARYMIME,CHUNKING,DSN.
/// </summary>
/// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this property is accessed.</exception>
/// <exception cref="ArgumentNullException">Is raised when null reference passed.</exception>
public string[] ServiceExtentions
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
return m_pServiceExtentions.ToArray();
}
set{
if(value == null){
throw new ArgumentNullException("ServiceExtentions");
}
m_pServiceExtentions.Clear();
foreach(string extention in value){
if(extention.ToUpper() == SMTP_ServiceExtensions.PIPELINING){
m_pServiceExtentions.Add(SMTP_ServiceExtensions.PIPELINING);
}
else if(extention.ToUpper() == SMTP_ServiceExtensions.SIZE){
m_pServiceExtentions.Add(SMTP_ServiceExtensions.SIZE);
}
else if(extention.ToUpper() == SMTP_ServiceExtensions.STARTTLS){
m_pServiceExtentions.Add(SMTP_ServiceExtensions.STARTTLS);
}
else if(extention.ToUpper() == SMTP_ServiceExtensions._8BITMIME){
m_pServiceExtentions.Add(SMTP_ServiceExtensions._8BITMIME);
}
else if(extention.ToUpper() == SMTP_ServiceExtensions.BINARYMIME){
m_pServiceExtentions.Add(SMTP_ServiceExtensions.BINARYMIME);
}
else if(extention.ToUpper() == SMTP_ServiceExtensions.CHUNKING){
m_pServiceExtentions.Add(SMTP_ServiceExtensions.CHUNKING);
}
else if(extention.ToUpper() == SMTP_ServiceExtensions.DSN){
m_pServiceExtentions.Add(SMTP_ServiceExtensions.DSN);
}
}
}
}
/// <summary>
/// Gets or sets server greeting text.
/// </summary>
/// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this property is accessed.</exception>
public string GreetingText
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
return m_GreetingText; }
set{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
m_GreetingText = value;
}
}
/// <summary>
/// Gets or sets how many bad commands session can have before it's terminated. Value 0 means unlimited.
/// </summary>
/// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this property is accessed.</exception>
/// <exception cref="ArgumentException">Is raised when invalid value is passed.</exception>
public int MaxBadCommands
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
return m_MaxBadCommands;
}
set{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
if(value < 0){
throw new ArgumentException("Property 'MaxBadCommands' value must be >= 0.");
}
m_MaxBadCommands = value;
}
}
/// <summary>
/// Gets or sets maximum mail transactions per session. Value 0 means unlimited.
/// </summary>
/// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this property is accessed.</exception>
/// <exception cref="ArgumentException">Is raised when invalid value is passed.</exception>
public int MaxTransactions
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
return m_MaxTransactions;
}
set{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
if(value < 0){
throw new ArgumentException("Property 'MaxTransactions' value must be >= 0.");
}
m_MaxTransactions = value;
}
}
/// <summary>
/// Gets or sets maximum message size in bytes.
/// </summary>
/// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this property is accessed.</exception>
/// <exception cref="ArgumentException">Is raised when invalid value is passed.</exception>
public int MaxMessageSize
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
return m_MaxMessageSize;
}
set{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
if(value < 500){
throw new ArgumentException("Property 'MaxMessageSize' value must be >= 500.");
}
m_MaxMessageSize = value;
}
}
/// <summary>
/// Gets or sets maximum allowed recipients per SMTP transaction.
/// </summary>
/// <remarks>According RFC 5321 this value SHOULD NOT be less than 100.</remarks>
/// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this property is accessed.</exception>
/// <exception cref="ArgumentException">Is raised when invalid value is passed.</exception>
public int MaxRecipients
{
get{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
return m_MaxRecipients;
}
set{
if(this.IsDisposed){
throw new ObjectDisposedException(this.GetType().Name);
}
if(value < 1){
throw new ArgumentException("Property 'MaxRecipients' value must be >= 1.");
}
m_MaxRecipients = value;
}
}
/// <summary>
/// Gets SMTP service extentions list.
/// </summary>
internal List<string> Extentions
{
get{ return m_pServiceExtentions; }
}
#endregion
#region Events implementation
#endregion
}
}