-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSimpleSmtpClient.cs
More file actions
698 lines (592 loc) · 18.2 KB
/
SimpleSmtpClient.cs
File metadata and controls
698 lines (592 loc) · 18.2 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Waher.Content;
using Waher.Content.Html;
using Waher.Content.Markdown;
using Waher.Content.Multipart;
using Waher.Content.Text;
using Waher.Events;
using Waher.Networking.SASL;
using Waher.Networking.SMTP.Exceptions;
using Waher.Networking.Sniffers;
using Waher.Security;
namespace Waher.Networking.SMTP
{
/// <summary>
/// Simple SMTP Client
/// </summary>
public class SimpleSmtpClient : CommunicationLayer, ISaslClientSide, IDisposableAsync
{
/// <summary>
/// 25
/// </summary>
public const int DefaultSmtpPort = 25;
/// <summary>
/// 587
/// </summary>
public const int AlternativeSmtpPort = 587;
private readonly List<KeyValuePair<int, string>> response = new List<KeyValuePair<int, string>>();
private TaskCompletionSource<KeyValuePair<int, string>[]> responseSource = new TaskCompletionSource<KeyValuePair<int, string>[]>();
private RowTcpClient client;
private readonly object synchObj = new object();
private readonly string userName;
private readonly string password;
private readonly string host;
private readonly int port;
private string domain;
private bool startTls = false;
//private bool smptUtf8 = false;
//private bool eightBitMime = false;
//private bool enhancedStatusCodes = false;
//private bool help = false;
private bool trustCertificate = false;
//private int? size = null;
private string[] authMechanisms = null;
private string[] permittedAuthenticationMechanisms = null;
/// <summary>
/// Simple SMTP Client
/// </summary>
/// <param name="Domain">Domain</param>
/// <param name="Host">Host name</param>
/// <param name="Port">Port number</param>
/// <param name="Sniffers">Sniffers</param>
public SimpleSmtpClient(string Domain, string Host, int Port, params ISniffer[] Sniffers)
: this(Domain, Host, Port, null, null, Sniffers)
{
}
/// <summary>
/// Simple SMTP Client
/// </summary>
/// <param name="Domain">Domain</param>
/// <param name="Host">Host name</param>
/// <param name="Port">Port number</param>
/// <param name="Sniffers">Sniffers</param>
/// <param name="UserName">User name</param>
/// <param name="Password">Password</param>
public SimpleSmtpClient(string Domain, string Host, int Port, string UserName, string Password, params ISniffer[] Sniffers)
: base(false, Sniffers)
{
this.domain = Domain;
this.host = Host;
this.port = Port;
this.userName = UserName;
this.password = Password;
}
/// <summary>
/// Connects to the server.
/// </summary>
public async Task Connect()
{
if (!(this.client is null))
{
await this.client.DisposeAsync();
this.client = null;
}
lock (this.synchObj)
{
this.response.Clear();
}
this.client = new RowTcpClient(Encoding.UTF8, 10000, false);
this.client.Client.ReceiveTimeout = 10000;
this.client.Client.SendTimeout = 10000;
this.client.OnReceived += this.Client_OnReceived;
this.client.OnSent += this.Client_OnSent;
this.client.OnError += this.Client_OnError;
this.client.OnInformation += this.Client_OnInformation;
this.client.OnWarning += this.Client_OnWarning;
this.Information("Connecting to " + this.host + ":" + this.port.ToString());
await this.client.ConnectAsync(this.host, this.port);
this.Information("Connected to " + this.host + ":" + this.port.ToString());
await this.AssertOkResult();
}
private string Client_OnWarning(string Text)
{
this.Warning(Text);
return Text;
}
private string Client_OnInformation(string Text)
{
this.Information(Text);
return Text;
}
private Task Client_OnError(object Sender, Exception Exception)
{
this.Error(Exception.Message);
return Task.CompletedTask;
}
private Task<bool> Client_OnSent(object Sender, string Text)
{
this.TransmitText(Text);
return Task.FromResult(true);
}
private Task<bool> Client_OnReceived(object Sender, string Row)
{
if (string.IsNullOrEmpty(Row))
{
this.Error("No response returned.");
return Task.FromResult(true);
}
this.ReceiveText(Row);
int i = Row.IndexOfAny(spaceHyphen);
if (i < 0)
i = Row.Length;
if (!int.TryParse(Row[..i], out int Code))
{
this.Error("Invalid response returned.");
return Task.FromResult(true);
}
bool More = i < Row.Length && Row[i] == '-';
lock (this.synchObj)
{
if (i < Row.Length)
Row = Row[(i + 1)..].Trim();
else
Row = string.Empty;
this.response.Add(new KeyValuePair<int, string>(Code, Row));
if (!More)
{
this.responseSource.TrySetResult(this.response.ToArray());
this.response.Clear();
}
}
return Task.FromResult(true);
}
/// <summary>
/// Disposes of the client.
/// </summary>
[Obsolete("Use DisposeAsync() instead.")]
public void Dispose()
{
this.DisposeAsync().Wait();
}
/// <summary>
/// <see cref="IDisposableAsync.DisposeAsync"/>
/// </summary>
public async Task DisposeAsync()
{
if (!(this.client is null))
{
await this.client.DisposeAsync();
this.client = null;
}
}
/// <summary>
/// Domain
/// </summary>
public string Domain => this.domain;
/// <summary>
/// If server certificate should be trusted by default (default=false).
/// </summary>
public bool TrustCertificate
{
get => this.trustCertificate;
set => this.trustCertificate = value;
}
/// <summary>
/// Server certificate.
/// </summary>
public X509Certificate ServerCertificate => this.client.RemoteCertificate;
/// <summary>
/// If server certificate is valid.
/// </summary>
public bool ServerCertificateValid => this.client.RemoteCertificateValid;
/// <summary>
/// Permitted authentication mechanisms.
/// </summary>
public string[] PermittedAuthenticationMechanisms
{
get => this.permittedAuthenticationMechanisms;
set => this.permittedAuthenticationMechanisms = value;
}
/// <summary>
/// Reads a response from the server.
/// </summary>
/// <returns>Response codes, and messages</returns>
public Task<KeyValuePair<int, string>[]> ReadResponse()
{
return this.ReadResponse(10000);
}
/// <summary>
/// Reads a response from the server.
/// </summary>
/// <param name="Timeout">Timeout, in milliseconds.</param>
/// <returns>Response codes, and messages</returns>
public async Task<KeyValuePair<int, string>[]> ReadResponse(int Timeout)
{
TaskCompletionSource<KeyValuePair<int, string>[]> Source = this.responseSource;
if (await Task.WhenAny(Source.Task, Task.Delay(Timeout)) != Source.Task)
throw new TimeoutException("Response not returned in time.");
return Source.Task.Result;
}
private static readonly char[] spaceHyphen = new char[] { ' ', '-' };
private Task WriteLine(string Row)
{
lock (this.synchObj)
{
this.response.Clear();
this.responseSource = new TaskCompletionSource<KeyValuePair<int, string>[]>();
}
return this.client.SendAsync(Row);
}
private Task Write(bool ConstantBuffer, byte[] Bytes)
{
return this.client.SendAsync(ConstantBuffer, Bytes);
}
private Task<string> AssertOkResult()
{
return this.AssertResult(300);
}
private Task<string> AssertContinue()
{
return this.AssertResult(400);
}
private async Task<string> AssertResult(int MaxExclusive)
{
KeyValuePair<int, string>[] Response = await this.ReadResponse();
int Code = Response[0].Key;
string Message = Response[0].Value.Trim();
if (string.IsNullOrEmpty(Message))
Message = "Request rejected.";
if (Code < 200 || Code >= MaxExclusive)
{
if (Code >= 400 && Code < 500)
throw new SmtpTemporaryErrorException(Message, Code);
else
throw new SmtpException(Message, Code);
}
return Response[0].Value;
}
/// <summary>
/// Sends the EHLO command.
/// </summary>
/// <param name="Domain">Domain</param>
/// <exception cref="IOException">If unable to execute command.</exception>
/// <exception cref="AuthenticationException">If transport authentication failed.</exception>
/// <returns>Domain name, as seen by server.</returns>
public async Task<string> EHLO(string Domain)
{
this.startTls = false;
//this.size = null;
this.authMechanisms = null;
//this.smptUtf8 = false;
//this.eightBitMime = false;
//this.enhancedStatusCodes = false;
//this.help = false;
if (string.IsNullOrEmpty(Domain))
await this.WriteLine("EHLO");
else
await this.WriteLine("EHLO " + Domain);
KeyValuePair<int, string>[] Response = await this.ReadResponse();
if (Response[0].Key < 200 || Response[0].Key >= 300)
throw new IOException("Request rejected.");
int i = Response[0].Value.LastIndexOf('[');
int j = Response[0].Value.LastIndexOf(']');
string ResponseDomain;
if (i >= 0 && j > i)
{
ResponseDomain = Response[0].Value.Substring(i + 1, j - i - 1);
if (string.IsNullOrEmpty(Domain))
Domain = ResponseDomain;
if (string.IsNullOrEmpty(this.domain))
this.domain = ResponseDomain;
}
else
ResponseDomain = string.Empty;
foreach (KeyValuePair<int, string> P in Response)
{
string s = P.Value.ToUpper();
switch (s)
{
case "STARTTLS":
this.startTls = true;
break;
case "SMTPUTF8":
//this.smptUtf8 = true;
break;
case "8BITMIME":
//this.eightBitMime = true;
break;
case "ENHANCEDSTATUSCODES":
//this.enhancedStatusCodes = true;
break;
case "HELP":
//this.help = true;
break;
default:
/*if (s.StartsWith("SIZE "))
{
if (int.TryParse(s.Substring(5).Trim(), out int i))
this.size = i;
}
else*/
if (s.StartsWith("AUTH "))
this.authMechanisms = s[5..].Trim().Split(space, StringSplitOptions.RemoveEmptyEntries);
break;
}
}
if (this.startTls && !(this.client.Stream is SslStream))
{
await this.WriteLine("STARTTLS");
await this.AssertOkResult(); // Will pause when complete.
await this.client.PauseReading();
this.Information("Starting TLS handshake.");
await this.client.UpgradeToTlsAsClient(null, Crypto.SecureTls, this.trustCertificate);
this.Information("TLS handshake complete.");
this.client.Continue();
ResponseDomain = await this.EHLO(Domain);
}
else if (!(this.authMechanisms is null) && !string.IsNullOrEmpty(this.userName) && !string.IsNullOrEmpty(this.password))
{
foreach (string Mechanism in this.authMechanisms)
{
if (Mechanism == "EXTERNAL")
continue;
SslStream SslStream = this.client.Stream as SslStream;
foreach (IAuthenticationMechanism M in SaslModule.Mechanisms)
{
if (M.Name != Mechanism)
continue;
if (!M.Allowed(SslStream))
break;
if (!(this.permittedAuthenticationMechanisms is null) &&
Array.IndexOf(this.permittedAuthenticationMechanisms, Mechanism) < 0)
{
break;
}
bool? b;
try
{
b = await M.Authenticate(this.userName, this.password, this);
if (!b.HasValue)
continue;
}
catch (Exception)
{
b = false;
}
if (!b.Value)
throw new AuthenticationException("Unable to authenticate user.");
return ResponseDomain;
}
}
throw new AuthenticationException("No suitable and supported authentication mechanism found.");
}
return ResponseDomain;
}
private static readonly char[] space = new char[] { ' ' };
/// <summary>
/// Executes the VRFY command.
/// </summary>
/// <param name="Account">Account name</param>
public async Task VRFY(string Account)
{
await this.WriteLine("VRFY " + Account);
await this.AssertOkResult();
}
/// <summary>
/// Executes the MAIL FROM command.
/// </summary>
/// <param name="Sender">Sender of mail.</param>
public async Task MAIL_FROM(string Sender)
{
await this.WriteLine("MAIL FROM: <" + Sender + ">");
await this.AssertOkResult();
}
/// <summary>
/// Executes the RCPT TO command.
/// </summary>
/// <param name="Receiver">Receiver of mail.</param>
public async Task RCPT_TO(string Receiver)
{
await this.WriteLine("RCPT TO: <" + Receiver + ">");
await this.AssertOkResult();
}
/// <summary>
/// Executes the QUIT command.
/// </summary>
public async Task QUIT()
{
await this.WriteLine("QUIT");
await this.AssertOkResult();
}
/// <summary>
/// Executes the DATA command.
/// </summary>
public Task DATA(KeyValuePair<string, string>[] Headers, byte[] Body)
{
return this.DATA(Headers, false, Body);
}
/// <summary>
/// Executes the DATA command.
/// </summary>
public async Task DATA(KeyValuePair<string, string>[] Headers, bool ConstantBody, byte[] Body)
{
await this.WriteLine("DATA");
await this.AssertContinue();
foreach (KeyValuePair<string, string> Header in Headers)
await this.WriteLine(Header.Key + ": " + Header.Value);
await this.WriteLine(string.Empty);
int c = Body.Length;
int i = 0;
int j;
while (i < c)
{
j = this.IndexOf(Body, crLfDot, i);
if (j < 0)
j = c;
if (i == 0 && j == c)
await this.Write(ConstantBody, Body);
else
{
byte[] Bin = new byte[j - i];
Buffer.BlockCopy(Body, i, Bin, 0, j - i);
await this.Write(true, Bin);
}
i = j;
if (i < c)
{
await this.Write(true, crLfDot);
i += 2;
}
}
await this.WriteLine(string.Empty);
await this.WriteLine(string.Empty);
await this.WriteLine(".");
await this.AssertOkResult();
}
private static readonly byte[] crLfDot = new byte[] { (byte)'\r', (byte)'\n', (byte)'.' };
private int IndexOf(byte[] Data, byte[] Segment, int StartIndex)
{
int i, j;
int d = Segment.Length;
int c = Data.Length - d + 1;
for (i = StartIndex; i < c; i++)
{
for (j = 0; j < d; j++)
{
if (Data[i + j] != Segment[j])
break;
}
if (j == d)
return i;
}
return -1;
}
/// <summary>
/// Initiates authentication
/// </summary>
/// <param name="Mechanism">Mechanism</param>
/// <param name="Parameters">Any parameters.</param>
/// <returns>If initiation was successful, challenge is returned.</returns>
public async Task<string> Initiate(IAuthenticationMechanism Mechanism, string Parameters)
{
string s = "AUTH " + Mechanism.Name;
if (!string.IsNullOrEmpty(Parameters))
s += " " + Parameters;
await this.WriteLine(s);
return await this.AssertContinue();
}
/// <summary>
/// Sends a challenge response back to the server.
/// </summary>
/// <param name="Mechanism">Mechanism</param>
/// <param name="Parameters">Any parameters.</param>
/// <returns>If challenge response was successful, response is returned.</returns>
public async Task<string> ChallengeResponse(IAuthenticationMechanism Mechanism, string Parameters)
{
await this.WriteLine(Parameters);
return await this.AssertContinue();
}
/// <summary>
/// Sends a final response back to the server.
/// </summary>
/// <param name="Mechanism">Mechanism</param>
/// <param name="Parameters">Any parameters.</param>
/// <returns>If final response was successful, response is returned.</returns>
public async Task<string> FinalResponse(IAuthenticationMechanism Mechanism, string Parameters)
{
await this.WriteLine(Parameters);
await this.AssertOkResult();
return null; // No response in SMTP
}
/// <summary>
/// Sends a formatted e-mail message.
/// </summary>
/// <param name="Sender">Sender of message.</param>
/// <param name="Recipient">Recipient of message.</param>
/// <param name="Subject">Subject</param>
/// <param name="MarkdownContent">Markdown content.</param>
/// <param name="Attachments">Any attachments.</param>
public async Task SendFormattedEMail(string Sender, string Recipient, string Subject,
string MarkdownContent, params object[] Attachments)
{
MarkdownDocument Doc = await MarkdownDocument.CreateAsync(MarkdownContent);
string HTML = "<html><body>" + HtmlDocument.GetBody(await Doc.GenerateHTML()) + "</body></html>";
string PlainText = await Doc.GeneratePlainText();
ContentAlternatives Content = new ContentAlternatives(new EmbeddedContent[]
{
new EmbeddedContent()
{
ContentType = "text/html; charset=utf-8",
Raw = Encoding.UTF8.GetBytes(HTML)
},
new EmbeddedContent()
{
ContentType = PlainTextCodec.DefaultContentType + "; charset=utf-8",
Raw = Encoding.UTF8.GetBytes(PlainText)
},
new EmbeddedContent()
{
ContentType = "text/markdown; charset=utf-8",
Raw = Encoding.UTF8.GetBytes(MarkdownContent)
}
});
ContentResponse P = await InternetContent.EncodeAsync(Content, Encoding.UTF8);
if (Attachments.Length > 0)
{
List<EmbeddedContent> Parts = new List<EmbeddedContent>()
{
new EmbeddedContent()
{
ContentType = P.ContentType,
Raw = P.Encoded
}
};
foreach (object Attachment in Attachments)
{
ContentResponse P2 = await InternetContent.EncodeAsync(Attachment, Encoding.UTF8);
Parts.Add(new EmbeddedContent()
{
ContentType = P2.ContentType,
Raw = P2.Encoded
});
}
MixedContent Mixed = new MixedContent(Parts.ToArray());
P = await InternetContent.EncodeAsync(Mixed, Encoding.UTF8);
}
byte[] BodyBin = P.Encoded;
string ContentType = P.ContentType;
List<KeyValuePair<string, string>> Headers = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("MIME-VERSION", "1.0"),
new KeyValuePair<string, string>("FROM", Sender),
new KeyValuePair<string, string>("TO", Recipient),
new KeyValuePair<string, string>("SUBJECT", Subject),
new KeyValuePair<string, string>("DATE", CommonTypes.EncodeRfc822(DateTime.Now)),
new KeyValuePair<string, string>("IMPORTANCE", "normal"),
new KeyValuePair<string, string>("X-PRIORITY", "3"),
new KeyValuePair<string, string>("MESSAGE-ID", Guid.NewGuid().ToString()),
new KeyValuePair<string, string>("CONTENT-TYPE", ContentType)
};
await this.MAIL_FROM(Sender);
await this.RCPT_TO(Recipient);
await this.DATA(Headers.ToArray(), true, BodyBin);
}
}
}