11

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("jane@contoso.com", "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": { }
}
Camille Laborde
  • 858
  • 11
  • 17
  • I don't believe that the `SmtpClient` is being ported to .Net Core. (You can use http://packagesearch.azurewebsites.net/ to find the new NuGet packages, and there isn't one.) Do you need .Net Core? – Matt DeKrey May 18 '15 at 12:56
  • 1
    No you're right : since it was included by default in project templates, I thought I needed it, but it works fine without it. Thanks ! – Camille Laborde May 20 '15 at 09:38
  • See https://github.com/dotnet/corefx/issues/1006 – Jon Hanna Dec 04 '15 at 11:26

4 Answers4

12

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.

Joe Audette
  • 35,330
  • 11
  • 106
  • 99
  • 2
    Hats off to Jeffrey Stedfast, but his work shouldn't have been necessary. E-mail is an important piece of most web applications: Registration confirmation, password recovery, forum response notifications, order confirmations, etc. The fact that we are in RC without a port of the mail classes is mildly disappointing. If a single developer can write MailKit, how can a team of bright Microsoft minds working on ASP.net not port the old code? To hamper Linux/OSX as viable alternatives? I'm digging a lot of the new functionality/features, but this seems like a pretty serious omission. – Sam Jan 04 '16 at 22:00
  • 4
    @Sam not to get into protracted discussion here on Stack Overflow, but I think the adage: *How do you eat an elephant? One bite at a time.* certainly applies to the work that Microsoft is doing on .NET Core. – ahsteele Feb 26 '16 at 20:00
  • See the .Net Core backlog on GitHub: https://github.com/dotnet/corefx/issues/1006 – Richard Nalezynski Oct 10 '16 at 15:53
5

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.

Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
1

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

0

If you have set up your project in Visual Studio 2017, then this solution will work:

  1. Edit the .csproj file of your project. Right-click on the project name and choose 'Edit Project1.csproj' (in case your project name is Project1). The project's XML file opens. Change the 'TargetFramework' to net452 and add a 'RuntimeIdentifier' tag with value win7-x86. So it looks like this:

    [PropertyGroup] [TargetFramework]net452[/TargetFramework] [RuntimeIdentifier]win7-x86[/RuntimeIdentifier] [/PropertyGroup]

(replace all brackets bij 'less than' and 'greater than' signs) Your project will be linked to the .NET Framework 4.5.2 and all libraries will be available then, including the System.NET.Mail namespace.

For me, this is a working solution...:-)