forked from cosullivan/SmtpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuitCommand.cs
More file actions
58 lines (52 loc) · 2 KB
/
Copy pathQuitCommand.cs
File metadata and controls
58 lines (52 loc) · 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
using System.Threading;
using System.Threading.Tasks;
using System.Net.Sockets;
using SmtpServer.IO;
using System.IO;
namespace SmtpServer.Protocol
{
/// <summary>
/// Quit Command
/// </summary>
public sealed class QuitCommand : SmtpCommand
{
/// <summary>
/// Smtp Quit Command
/// </summary>
public const string Command = "QUIT";
/// <summary>
/// Constructor.
/// </summary>
public QuitCommand() : base(Command) { }
/// <summary>
/// Execute the command.
/// </summary>
/// <param name="context">The execution context to operate on.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Returns true if the command executed successfully such that the transition to the next state should occurr, false
/// if the current state is to be maintained.</returns>
internal override async Task<bool> ExecuteAsync(SmtpSessionContext context, CancellationToken cancellationToken)
{
context.IsQuitRequested = true;
try
{
await context.Pipe.Output.WriteReplyAsync(SmtpResponse.ServiceClosingTransmissionChannel, cancellationToken).ConfigureAwait(false);
}
catch (IOException ioException)
{
if (ioException.GetBaseException() is SocketException socketException)
{
// Some mail servers will send the QUIT command and then disconnect before
// waiting for the 221 response from the server. This doesnt follow the spec but
// we can gracefully handle this situation as in theory everything should be fine
if (socketException.SocketErrorCode == SocketError.ConnectionReset)
{
return true;
}
}
throw ioException;
}
return true;
}
}
}