How to configure email settings in ASP.NET 5? -
how configure mail settings <system.net><mailsettings>
used in web.config
in asp.net 4? guess need call services.configure<>()
have no idea options should pass. ideas?
thanks, f0rt
try way keep secrets secret. first, install secretmanager
, configure secrets. when you're on local machine, you'll want use values secretmanager
. hosting (e.g. in azure) you'll use environmental variables.
local: install , configure 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 bug makes go awry if have vs 2015 rc installed. there's workaround here.
live: use environmental variables
here's example in ms azure web app, though other web hosts have similar options.
project.json
note we're targeting dnx451
. also, have 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 can access user secrets locally, , when environmental variables overwrite them when available. i've tested minimal project. 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(); // order cascades // e.g. environmental variables overwrite 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 = "me@domain.com"; var = "you@domain.com"; var subject = "dinner on tues?"; var body = "how 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); }); } } }
Comments
Post a Comment