File tree Expand file tree Collapse file tree 1 file changed +8
-19
lines changed
docs/extensibility/snippets/MailDevResourceAndComponent/MailKit.Client Expand file tree Collapse file tree 1 file changed +8
-19
lines changed Original file line number Diff line number Diff line change @@ -11,9 +11,6 @@ namespace MailKit.Client;
11
11
/// </param>
12
12
public sealed class MailKitClientFactory ( MailKitClientSettings settings ) : IDisposable
13
13
{
14
- private readonly SemaphoreSlim _semaphore = new ( 1 , 1 ) ;
15
-
16
- private SmtpClient ? _client ;
17
14
18
15
/// <summary>
19
16
/// Gets an <see cref="ISmtpClient"/> instance in the connected state
@@ -29,29 +26,21 @@ public sealed class MailKitClientFactory(MailKitClientSettings settings) : IDisp
29
26
public async Task < ISmtpClient > GetSmtpClientAsync (
30
27
CancellationToken cancellationToken = default )
31
28
{
32
- await _semaphore . WaitAsync ( cancellationToken ) ;
33
-
29
+ var client = new SmtpClient ( ) ;
34
30
try
35
31
{
36
- if ( _client is null )
32
+ if ( settings . Endpoint is not null )
37
33
{
38
- _client = new SmtpClient ( ) ;
39
-
40
- await _client . ConnectAsync ( settings . Endpoint , cancellationToken )
34
+ await client . ConnectAsync ( settings . Endpoint , cancellationToken )
41
35
. ConfigureAwait ( false ) ;
42
36
}
37
+ return client ;
43
38
}
44
- finally
39
+ catch
45
40
{
46
- _semaphore . Release ( ) ;
41
+ await client . DisconnectAsync ( true , cancellationToken )
42
+ client. Dispose ( ) ;
43
+ throw ;
47
44
}
48
-
49
- return _client ;
50
- }
51
-
52
- public void Dispose ( )
53
- {
54
- _client ? . Dispose ( ) ;
55
- _semaphore . Dispose ( ) ;
56
45
}
57
46
}
You can’t perform that action at this time.
0 commit comments