How to configure email settings in ASP.NET 5?

2.9k views Asked by At

How to configure mail settings <system.net><mailSettings> that used to be in web.config in asp.net 4? I guess I need to call services.Configure<>() but I have no idea what options I should pass. Any ideas?

Thanks, f0rt

1

There are 1 answers

1
Shaun Luttin On BEST ANSWER

Try this as a way to keep your secrets secret. First, install the SecretManager and configure it with your secrets. When you're on your local machine, you'll want to use values from the SecretManager. With your hosting (e.g. in Azure) you'll use environmental variables.

Local: Install and Configure the 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"

There's a bug that makes this go awry if you have VS 2015 RC installed. There's a workaround here.

Live: Use Environmental Variables

Here's an example in a MS Azure Web App, though other web hosts probably have similar options.

Web App > Dashboard > Configure

project.json

Note that we're targeting only dnx451. Also, we have a 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

Now you can access those user secrets locally, and when environmental variables will overwrite them when available. I've just tested this minimal project. It works.

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 = "[email protected]";
            var to = "[email protected]";
            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);
            });
        }
    }
}

See also:

DNX Secret Configuration

Rick Anderson's Draft on App Secrets