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.

SmtpClient client = new SmtpClient("mysmtpserver");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");

MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("");
mailMessage.Body = "body";
mailMessage.Subject = "subject";

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 :

<Project Sdk="Microsoft.NET.Sdk">

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

16 thoughts on “Sending Email In .NET Core 2.0”

  1. I see nothing for any of the frameworks in 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: : HOSTING RELAY : LNxdfYhdnUJxse : DED : ESMTP No Relay Quota information retrieved. Please try again.’

    Any Ideas?

    Thank you very much.

    • 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.

    • 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 :

      Sorry I can’t be more help!

  4. Hey, Wade, thanks for the content.
    Seems like this is the exact same as in the full framework.
    I’ve read that some people said this was marked as obsolete. It’s not. Not in the Core implementation, nor in the docs. They must have changed it or something.

    Well, regarding the code, I just had to enable ssl because it would throw an exception when sending an e-mail with hotmail.

    Thanks, again.

  5. Thanks for writing this post – worked as you said it would.
    Will get round to looking at MailKit at some point but this will be fine in the meanwhile.


Leave a Comment