Sending Email In .NET Core 2.0

Because the legacy SmtpClient inside .NET Core is now marked as deprecated. It is recommended you follow our guide on integrating your .NET code with the MailKit library in our tutorial here!


In a previous post, I wrote about how there was no way to send email on .NET Core. In version 1.0 of the framework and 1.6 of the standard, the SMTP client code in .NET was not yet ported over , that is until the release of .NET Core 2.0.

With things ported over, the interfaces and classes are virtually identical to ones you might have used in the full framework. Consider the following written in .NET Core 2.0.

For the most part, if you had code that could send email via SMTP in the full framework, it’s likely a matter of a copy and paste job to get it going in .NET Core now!

If you are having issues with this, ensure that you are on .NET Core 2.0 framework. You can check this by editing your csproj file, it should look like the following :

Where TargetFramework is set to 2.0. Anything lower and you will not have access to the SmtpClient class!

11 comments

  1. I see nothing for any of the frameworks in docs.microsoft.com regarding deprecation or obsolescence of SmtpClient. Did something change?

  2. Hi all,

    I am trying to make this work but so far it doesn’t. The message I get is:

    System.Net.Mail.SmtpException: Service not available, closing transmission channel.
    The server response was: xxxxxxxxxxx.prod.phx3.secureserver.net : HOSTING RELAY : LNxdfYhdnUJxse : DED : ESMTP No Relay Quota information retrieved. Please try again.’

    Any Ideas?

    Thank you very much.

    1. Hi Sorin,

      It’s likely that it’s something to do with your SMTP server you are connecting to. For example it may require SSL and you are connecting without it (Or vice versa). I would carefully check the ports/ssl settings given to you by your mail provider and ensure they all match up.

  3. Hi Wade,

    Thank you for the message. I have the same code used with .Net code 4.5. and its working flawlessly.
    What I noticed is that I get the same error message if I put bogus credentials instead of good ones.
    Its the credentials that are not passed the right way.
    Any ideas? Thank you for the time.
    Sorin

    1. Hi Sorin,

      If you can copy and paste the exact same code into full framework and it works, then that’s definitely interesting!

      Seems like it could be a bug in the .NET Core implementation of the SMTP client. I would recommend logging an issue directly on the .NET Core FX repo with the code and state it works in .NET Framework but not on .NET Core (And include as much info about the SMTP you are using). You can do that here : https://github.com/dotnet/corefx

      Sorry I can’t be more help!

Leave a Reply

Your email address will not be published. Required fields are marked *