ame*_*lon 16 c# asp.net-core-mvc asp.net-core
我正在涉足ASP 5/MVC 6组合,我发现我不再知道如何做最简单的事情.例如,您如何发送电子邮件?
在MVC 5世界中,我会做这样的事情:
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);
}
Run Code Online (Sandbox Code Playgroud)
现在这段代码不再编译,System.Net.Mail
似乎不再存在.在互联网上进行一些讨论之后,它似乎不再包含在新的核心(dnxcore50
)中.这让我想到了我的问题......
你如何在新世界发送电子邮件?
还有一个更大的问题,你在哪里可以找到核心.Net中不再包含的所有东西的替代品?
jst*_*ast 27
我的开源MimeKit和MailKit库现在支持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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
.NET Core 目前缺少几个 API。System.Net.Mail.SmtpClient
正如您所发现的,这些内容System.ServiceModel.SyndicationFeed
也包括可用于构建 RSS 或 Atom 提要的内容。此问题的解决方法是针对完整的 .NET Framework,而不是 .NET Core。一旦这些 API 可用,您就可以随时以 .NET Core 为目标。
因此,在您的 project.json 文件中,您需要删除对 .NET 4.5.1 或.NET 4.6的引用dnxcore50
并添加该引用(如果尚不存在):dnx451
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": {
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8636 次 |
最近记录: |