Skip to content

Commit

Permalink
[release/2.0.0] Port SmtpClient fixes to 2.0 (dotnet#24727)
Browse files Browse the repository at this point in the history
* Fix null ref exception when Dispose'ing SmtpClient (dotnet#24521)

If SmtpClient is disposed after a failed send and no successful sends, it attempts to call Close on a null NetworkStream field.

* Fix hang when SmtpClient.SendAsync fails to connect (dotnet#24536)

If Send{Mail}Async fails to connect, an exception gets thrown/eaten, and the completion callback is never invoked / the returned Task is never completed.
  • Loading branch information
Priya91 authored and Petermarcu committed Oct 30, 2017
1 parent 6ae66ab commit f997049
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ internal void ReleaseConnection()
_channelBindingToken.Close();
}

_networkStream.Close();
_networkStream?.Close();
_tcpClient.Dispose();
}

Expand Down Expand Up @@ -439,11 +439,11 @@ private void InitializeConnection()
IAsyncResult result = _connection.BeginInitializeConnection(_host, _port, InitializeConnectionCallback, this);
if (result.CompletedSynchronously)
{
_connection.EndInitializeConnection(result);
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Connect returned");

try
{
_connection.EndInitializeConnection(result);
if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Connect returned");

Handshake();
}
catch (Exception e)
Expand All @@ -458,11 +458,11 @@ private static void InitializeConnectionCallback(IAsyncResult result)
if (!result.CompletedSynchronously)
{
ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
thisPtr._connection.EndInitializeConnection(result);
if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"Connect returned {thisPtr}");

try
{
thisPtr._connection.EndInitializeConnection(result);
if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"Connect returned {thisPtr}");

thisPtr.Handshake();
}
catch (Exception e)
Expand Down
18 changes: 18 additions & 0 deletions src/System.Net.Mail/tests/Functional/SmtpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@ public void TestTimeout(int value)
Assert.Equal(value, Smtp.Timeout);
}

[Fact]
public void Send_ServerDoesntExist_Throws()
{
using (var smtp = new SmtpClient(Guid.NewGuid().ToString("N")))
{
Assert.Throws<SmtpException>(() => smtp.Send("[email protected]", "[email protected]", "subject", "body"));
}
}

[Fact]
public async Task SendAsync_ServerDoesntExist_Throws()
{
using (var smtp = new SmtpClient(Guid.NewGuid().ToString("N")))
{
await Assert.ThrowsAsync<SmtpException>(() => smtp.SendMailAsync("[email protected]", "[email protected]", "subject", "body"));
}
}

[Fact]
public void TestMailDelivery()
{
Expand Down

0 comments on commit f997049

Please sign in to comment.