簡體   English   中英

ASP 5,MVC 6發送電子郵件

[英]ASP 5, MVC 6 sending email

我正在涉足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);
}

現在這個代碼不再編譯為System.Net.Mail似乎不再存在。 在互聯網上進行一些討論之后,它似乎不再包含在新核心( dnxcore50 )中。 這讓我想到了我的問題......

你如何在新世界發送電子郵件?

還有一個更大的問題,你在哪里可以找到核心.Net中不再包含的所有東西的替代品?

我的開源MimeKitMailKit庫現在支持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 Core目前有幾個缺失的API。 這些包括您找到的System.Net.Mail.SmtpClient以及System.ServiceModel.SyndicationFeed ,它們也可用於構建RSS或Atom訂閱源。 解決方法是針對完整的.NET Framework而不是.NET Core。 一旦這些API可用,您就可以始終以.NET Core為目標。

因此,在您project.json文件,你需要刪除提及dnxcore50並添加dnx451為.NET 4.5.1或dnx46用於.NET 4.6,如果它已不存在:

"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": {
    }
  }
}

System.Net.Mail現已移植到.NET Core。 請參閱corefx repo中的問題11792 此更改將成為.NET Standard 2.0的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM