I am dabbling with ASP 5/MVC 6 combination and I find that I no longer know how to do the simplest things. For example how do you send an email?
In MVC 5 world I would do something like this:
using (var smtp = new SmtpClient("localhost"))
{
var mail = new MailMessage
{
Subject = subject,
From = new MailAddress(fromEmail),
Body = message
};
mail.To.Add(toEmail);
await smtp.SendMailAsync(mail);
}
Now this code no longer compiles as System.Net.Mail
seems to no longer exist. After some poking around the internet it seems it's no longer include in the new core (dnxcore50
). Which brings me to my question...
How do you send an email in the new world?
And a larger question of where do you find substitutes for all the things that are no longer include in core .Net?
My open source MimeKit and MailKit libraries now support dnxcore50, which provide a really nice API for creating and sending email. As an added bonus, MimeKit supports DKIM signatures which is becoming more and more a must-have feature.
using System;
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;
namespace TestClient {
class Program
{
public static void Main (string[] args)
{
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey Tribbiani", "[email protected]"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "[email protected]"));
message.Subject = "How you doin'?";
message.Body = new TextPart ("plain") {
Text = @"Hey Chandler,
I just wanted to let you know that Monica and I were going to go play some paintball, you in?
-- Joey"
};
using (var client = new SmtpClient ()) {
client.Connect ("smtp.friends.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate ("joey", "password");
client.Send (message);
client.Disconnect (true);
}
}
}
}
.NET Core has several missing API's at the moment. These include System.Net.Mail.SmtpClient
as you have found and also System.ServiceModel.SyndicationFeed
too which can be used to build RSS or Atom feeds. The workaround for this is to target the full .NET Framework, instead of .NET Core. You can always target .NET Core once these API's become available.
So in your project.json file you need to remove the reference to dnxcore50
and to add dnx451
for .NET 4.5.1 or dnx46
for .NET 4.6 if it is not already there:
"frameworks": {
"dnx451": {
"frameworkAssemblies": {
"System.ServiceModel": "4.0.0.0"
// ..Add other .NET Framework references.
}
},
// Remove this to stop targeting .NET Core.
// Note that you can't comment it out because project.json does not allow comments.
"dnxcore50": {
"dependencies": {
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With