Runtime: Cannot catch exception on SmtpClient.SendMailAsync when no server is listening

Created on 20 Nov 2017  路  9Comments  路  Source: dotnet/runtime

See repo here
https://github.com/MrAntix/cant-catch-exception

I need to be able to catch an exception when the emailing fails for what ever reason

area-System.Net bug

All 9 comments

if I turn off only my own code and debug I see the exception

Exception thrown: 'System.Net.Internals.SocketExceptionFactory.ExtendedSocketException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Internals.SocketExceptionFactory.ExtendedSocketException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Internals.SocketExceptionFactory.ExtendedSocketException' in System.Private.CoreLib.dll
Exception thrown: 'System.Net.Internals.SocketExceptionFactory.ExtendedSocketException' in System.Private.CoreLib.dll
An unhandled exception of type 'System.Net.Internals.SocketExceptionFactory.ExtendedSocketException' occurred in System.Private.CoreLib.dll
No connection could be made because the target machine actively refused it

See repo here

I get a 404 from GitHub. Is the repo public?

it is now :) sorry about that

there is not much there in any case, its an xunit test project with one test

```c#
public class try_to_catch
{
public const int NO_SERVER_PORT = 26;

    [Fact]
    public async Task when_no_server()
    {
        try
        {
            using (var smtp = new System.Net.Mail.SmtpClient("localhost", NO_SERVER_PORT))
            {
                var message = new MailMessage(
                    "from@localhost", "to@localhost",
                    "Message Subject",
                    "Message Body");

               await smtp.SendMailAsync(message);
            }
        }
        catch (Exception)
        {
            Assert.True(true, "success");
        }
    }
}

```

[EDIT] Add C# syntax highlighting by @karelz

Thanks. I believe this was already fixed with https://github.com/dotnet/corefx/pull/24536. And I can repro your issue on 2.0 but not on 2.1.

any chance it can be patched in 2.0?

basically crashes dotnet which will make it difficult for anyone writing a reliable service which sends email

Fixed by dotnet/corefx#24536, closing. Feel free to reopen if that is not the case for you.

any chance it can be patched in 2.0?

It already was, in https://github.com/dotnet/corefx/pull/24727. That should be part of whenever the next 2.0 update happens.

I am able to catch that exception now, many thanks

Product Information: Version: 2.1.4
Microsoft .NET Core Shared Framework Host Version : 2.0.5

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bencz picture bencz  路  3Comments

matty-hall picture matty-hall  路  3Comments

chunseoklee picture chunseoklee  路  3Comments

jamesqo picture jamesqo  路  3Comments

Timovzl picture Timovzl  路  3Comments