How to set mailSettings from Azure App Service - A

2019-09-13 19:43发布

The current settings for the email server is checked into version control.

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network">
        <network host="..." port="25" password="..." userName="..." />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

I'd like to remove (at least the password) from version control so other developers can't (accidently) send mail over our mail server.

We're using a SAAS provider for our outbound email, because of this I cannot limit access to this server by setting up a firewall. The only authentication is using username and password provided by the SAAS provider.

Is it possible to have the mailSettings be set from the Application settings blade in Azure. This way I can remove the password from version control and do not need to manually update the web.config upon (automatic) deployment.

1条回答
We Are One
2楼-- · 2019-09-13 20:27

As you mentioned you are using Postal. On their GitHub, I notice you can provide a function that creates a SmtpClient.

Starting from there, you can 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);

This way you'll be able to modify your email settings in the Application settings of your Azure App Service.

查看更多
登录 后发表回答