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("firstname.lastname@example.org"); mailMessage.To.Add("email@example.com"); 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!