如何在ASP.NET 5 中配置电子邮件设置?

How to configure email settings in ASP.NET 5?

如何配置邮件设置 <system.net><mailSettings> 以前在 web.config 中 asp.net 4?我想我需要打电话给 services.Configure<>() 但我不知道我应该通过什么选项。有什么想法吗?

谢谢, f0rt

试试这个来保守秘密。首先,安装 SecretManager 并用你的秘密配置它。当您在本地计算机上时,您会希望使用 SecretManager 中的值。通过托管(例如在 Azure 中),您将使用环境变量。

本地:安装并配置 SecretManager

dnu commands install Microsoft.Extensions.SecretManager
user-secret set "smtp-host" "smtp-mail.outlook.com"
user-secret set "smtp-port" "587"
user-secret set "smtp-username" "myUsername"
user-secret set "smtp-password" "@#$HFS%#$%SFsd"

如果您安装了 VS 2015 RC,则有一个错误会导致此错误。 There's a workaround here.

实时:使用环境变量

这是 MS Azure Web 应用程序中的一个示例,尽管其他 Web 主机可能有类似的选项。

project.json

请注意,我们仅针对 dnx451。另外,我们有一个 userSecretsId

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",
  "userSecretsId" : "Add-an-arbitrary-user-secrets-id",

  "dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
    "Microsoft.Framework.ConfigurationModel.UserSecrets": "1.0.0-beta4"
  },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
  },

  "frameworks": {
    "dnx451": { }
  }

  /* other configuration omitted */

}

Startup.cs

现在您可以在本地访问这些用户机密,并且环境变量将在可用时覆盖它们。我刚刚测试了这个最小的项目。有效。

using System;
using System.Net;
using System.Net.Mail;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.ConfigurationModel;

namespace WebApplication1
{
    public class Startup
    {
        public Startup()
        {
            var configuration = new Configuration();

            // the order cascades 
            // e.g. Environmental variables will overwrite the UserSecrets
            configuration.AddUserSecrets();
            configuration.AddEnvironmentVariables();

            this.Configuration = configuration;
        }

        IConfiguration Configuration { get; set; }

        public void Configure(IApplicationBuilder app)
        {
            var host = this.Configuration.Get("smtp-host");
            var port = this.Configuration.Get("smtp-port");
            var username = this.Configuration.Get("smtp-username");
            var password = this.Configuration.Get("smtp-password"); 

            var from = "me@domain.com";
            var to = "you@domain.com";
            var subject = "Dinner on Tues?";
            var body = "How about it?";
            var mailMessage = new MailMessage(from, to, subject, body);

            var smtpClient = new SmtpClient();
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Host = host;
            smtpClient.Port = Int32.Parse(port);
            smtpClient.Credentials = new NetworkCredential(username, password);
            smtpClient.EnableSsl = true;

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello SMTP!");
                smtpClient.Send(mailMessage);
            });
        }
    }
}

另请参阅:

DNX Secret Configuration

Rick Anderson's Draft on App Secrets