Reputation: 16224
I usually put my SMTP parameters for ASP.NET applications in the <mailsettings>
element under <system.net>
in web.config.
Is there a way to configure these at the Azure portal under Application Settings or somewhere, or do I have to manage by editing web.config?
I do not mean reading from appSetting
keys and then stuffing them into the respective properites of System.Net.Mail
objects. I want the parameters, example host, port, credentials, sender, etc, to be automatically filled from the configuration file (<mailSettings>
element) when SmtpClient
is instantiated.
Upvotes: 4
Views: 1267
Reputation: 15571
I do not mean reading from
appSetting
keys and then stuffing them into the respective properites ofSystem.Net.Mail
objects. I want the parameters, example host, port, credentials, sender, etc, to be automatically filled from the configuration file whenSmtpClient
is instantiated.
No, you cannot.
There is an alternate solution, however. Have a look at the App Service Application Settings. (Any setting that's in there overrides the setting with the same name in the web.config.)
Since you're using the auto-config stuff of the mailSettings
element, you're going to have to change your code to not rely on the automatic configuration. Read the settings yourself and apply them to your SmpClient
.
You could create a custom SmtpClient
that will be built using application settings:
var smtpServerHost = CloudConfigurationManager.GetSetting("SmtpServerHost");
var smtpServerPort = int.Parse(CloudConfigurationManager.GetSetting("SmtpServerPort"));
var smtpServerUserName = CloudConfigurationManager.GetSetting("SmtpServerUserName");
var smtpServerPassword = CloudConfigurationManager.GetSetting("SmtpServerPassword");
var client = new SmtpClient(smtpServerHost, smtpServerPort);
client.Credentials = new NetworkCredential(smtpServerUserName, smtpServerPassword);
(inspired on this post: How to set mailSettings from Azure App Service - Application settings )
Upvotes: 1