Using System.Net.Mail in ASP NET MVC 6 project
Asked Answered
C

4

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("[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": { }
}
Cheyennecheyne answered 18/5, 2015 at 9:33 Comment(3)
I don't believe that the SmtpClient is being ported to .Net Core. (You can use packagesearch.azurewebsites.net to find the new NuGet packages, and there isn't one.) Do you need .Net Core?Ingrowing
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 !Cheyennecheyne
See github.com/dotnet/corefx/issues/1006Archon
I
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.

Ingrowing answered 20/5, 2015 at 14:0 Comment(0)
E
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.

Ericson answered 5/12, 2015 at 21:48 Comment(3)
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.Swore
@Swore 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.Shiva
See the .Net Core backlog on GitHub: github.com/dotnet/corefx/issues/1006Transmontane
I
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.

Ingrowing answered 20/5, 2015 at 14:0 Comment(0)
T
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

Transmontane answered 10/10, 2016 at 15:56 Comment(0)
R
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...:-)

Refined answered 31/5, 2017 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.