Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.Net.Mail in ASP NET MVC 6 project

I have trouble creating a simple mock mail sender within an ASP NET 5 project.

Here the method :

    public static Task SendMail(string Email, string Subject, string Body)
    {
        SmtpClient client = new SmtpClient();
        client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
        client.PickupDirectoryLocation = "C:\\TMP";

        MailAddress from = new MailAddress("[email protected]", "Jane " + (char)0xD8 + " Clayton", System.Text.Encoding.UTF8);
        MailAddress to = new MailAddress(Email);

        MailMessage message = new MailMessage(from, to);
        message.Body = Body;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = Subject;
        message.SubjectEncoding = System.Text.Encoding.UTF8;

        client.Send(message);
        message.Dispose();

        return Task.FromResult(0);
    }

I have included the dependency 'System.Net.Mail', but a tooltip says that the library is available in DNX 4.5.1 but not in DNX Core 5.0, and the project will not compile.

In my project.json there is :

"frameworks": {
    "dnx451": { },
    "dnxcore50": { }
}
like image 274
Camille Laborde Avatar asked May 18 '15 09:05

Camille Laborde


3 Answers

As of today, it is now possible to send email on dnxcore50 in addition to net451 (and lots of other platforms) using MailKit

I'm using rc1 of ASP.NET 5 and I added these dependencies to my project.json to bring in the needed nugets

"MimeKit": "1.3.0-beta1",
"MailKit": "1.3.0-beta1"

So the lack of System.Net.Mail in dnxcore50 is no longer a problem, the MailKit library is actually much more feature rich than any of the old System.Net.Mail stuff.

Huge Thanks to Jeffrey Stedfast for his hard work.

like image 134
Joe Audette Avatar answered Oct 09 '22 23:10

Joe Audette


I don't believe that the SmtpClient is being ported to .Net Core. (You can use the unofficial reverse package lookup to find the new NuGet packages, and there isn't one.)

Since you don't need .Net Core, you can remove the dnxcore50 entry from your frameworks in your project.json.

like image 42
Matt DeKrey Avatar answered Oct 09 '22 21:10

Matt DeKrey


For .NET Core be sure to obtain MailKit by updating project.json to add it as a dependency...

"MailKit": "~1.6.0"

See MailKit on GitHub for a complete example: https://github.com/jstedfast/MailKit

like image 41
Richard Nalezynski Avatar answered Oct 09 '22 23:10

Richard Nalezynski