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("[email protected]"); mailMessage.To.Add("[email protected]"); mailMessage.Body = "body"; mailMessage.Subject = "subject"; client.Send(mailMessage);
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"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> </Project>
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”
According to Microsoft:
[System.Obsolete(“SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead”)]
Interesting! You know what’s weird… The .NET Core implementation of SmtpClient is NOT marked as Obsolete… https://github.com/dotnet/corefx/blob/86d9e8fc0296d19779bc7f70127026a24fc75e4e/src/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs . Oversight or is the new implementation “better” in .NET Core? Such is the .NET lottery now.
It is marked as obsolete in the docs though.
So it is! Thanks Aaron!
Hey guys! Because it’s now marked as deprecated, I’ve put together a quick guide on getting up and running with the MailKit library. Check it out here : https://dotnetcoretutorials.com/2017/11/02/using-mailkit-send-receive-email-asp-net-core/
I see nothing for any of the frameworks in docs.microsoft.com regarding deprecation or obsolescence of SmtpClient. Did something change?
Apparently so, someone made an update to the documentation to remove the obsolete tag : https://github.com/dotnet/docs/commit/7ef4e82ea518a756b1a0d1d1684dde15653845aa#diff-aac793a10e9e8d7daa604332765b29db
I would still recommend using MailKit if you can. It handles much more scenarios and irons out plenty of edgecases.
It was an accident due to automated doc generation. Fortunately MailKit was just released with true async support, so now it’s definitely a better choice.
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.’
Thank you very much.
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.
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.
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!
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 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.
So according to Karel Zikmund from the MS .NET Core Team, the SmtpClient *IS* actually obsolete. According to him, there are no plans to maintain or fix this code in the future. It was purposely NOT marked in the code as ‘Obsolete’ so not to break existing projects that treat “Warnings as Errors”.