C# ASP 5,MVC 6发送电子邮件
我正在尝试ASP 5/MVC6组合,我发现我不再知道如何做最简单的事情。例如,如何发送电子邮件 在MVC 5世界中,我会这样做:C# ASP 5,MVC 6发送电子邮件,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我正在尝试ASP 5/MVC6组合,我发现我不再知道如何做最简单的事情。例如,如何发送电子邮件 在MVC 5世界中,我会这样做: using (var smtp = new SmtpClient("localhost")) { var mail = new MailMessage { Subject = subject, From = new MailAddress(fromEmail), Body = message };
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);
}
现在这段代码不再编译,因为System.Net.Mail
似乎不再存在。在浏览了一些互联网之后,它似乎不再包含在新的核心(dnxcore50
)中。这就引出了我的问题
在新世界里,你如何发送电子邮件
还有一个更大的问题是,在哪里可以找到不再包含在core.Net中的所有东西的替代品?.Net core目前缺少几个API。其中包括您已经找到的
System.Net.Mail.SmtpClient
,还包括System.ServiceModel.SyndicationFeed
,可用于构建RSS或Atom提要。解决方法是针对完整的.NET框架,而不是.NET核心。一旦这些API可用,您就可以始终以.NET内核为目标
因此,在project.json文件中,您需要删除对dnxcore50
的引用,并为.NET 4.5.1添加dnx451
,或为.NET 4.6添加dnx46
,如果尚未添加:
"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": {
}
}
}
我的开源和库现在支持dnxcore50,它为创建和发送电子邮件提供了一个非常好的API。此外,MimeKit还支持DKIM签名,这正成为越来越多的必备功能
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", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "chandler@friends.com"));
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.Mail现在已移植到.Net核心。请参见《核心外汇回购协议》。这一变化将是有趣的一部分。。你看过这个git页面了吗?在asp.net 5中,您不局限于以dnxcore50为目标。您仍然可以针对完整的.net框架(或mono)。你的目标是什么平台;Windows、Linux?我想/希望以后会有一批与dnxcore50兼容的mailkit,这就是我计划在我的应用程序中发送电子邮件的东西,但在.net core从betaIt推出之前,可能不会有很多第三方的东西可用。看起来它已经不复存在了,因为它甚至不在状态页面上,即使一切基本上都“完成”。我应该安装哪个版本?我试图在我的ASP.Net Core 1类库中安装1.3.0-beta6,但出现错误“依赖项MailKit 1.3.0-beta6不支持framework.NetPlatform,Version=v5.4”MailKit需要。NetPlatform v5.5,您的目标似乎是v5.4。我认为这是默认情况下RC1模板设置的内容,我认为在RC2问世之前,很多人也会把它作为目标。正如githit上所说的,你不能支持v5.4,所以现在就去寻找一些有效的东西。MailKit是唯一一个支持dotnet core的邮件库。