This topic is locked

SMTP Settings - Read from File not hard coded

3/14/2019 10:50:33 AM
ASPRunner.NET General questions
D
DennisB author

I recently learned that when you set up email - for registration, forgotten email, after events and so on, the server, username and password are written to a file appsettings - which made sense to me. I would expect, just like changing database connection information - in the web.config file - this app setting file would be where a customer could edit a file to enter their smtp info - their username, their password etc. This is not the case. The Email settings are read from the dll file created when you compile your ASPRunner web application - why bother writing them to a configuration file was my first thought. Is anyone developing web applications for others, customers maybe, to run from their own web servers rather than one you might provide? Have you found a way to allow others to Edit their email settings themselves? Rather than supply you their email credentials and have you compile and redeploy your application.
So my concern is I want to supply a web application with generic or sample credentials (like "your username" and "Your Password" and "Your Server Name" and "Your port" that could be edited by the customer, and I don't want to have to recompile with their information embedded in a .dll and say here, replace the dll of the same name.
Does anyone else find this a problem or concern? Have a suggestion?
Thanks

admin 3/14/2019

There are many ways to make it work. Nobody ever asked it, I guess this is not a common setup setup.
SMTP settings are being set in include/appsettings.cs file

GlobalVars.globalSettings.InitAndSetArrayItem(false, "useCustomSMTPSettings");

GlobalVars.globalSettings.InitAndSetArrayItem("", "strSMTPUser");

GlobalVars.globalSettings.InitAndSetArrayItem("localhost", "strSMTPServer");

GlobalVars.globalSettings.InitAndSetArrayItem("25", "strSMTPPort");

GlobalVars.globalSettings.InitAndSetArrayItem("", "strSMTPPassword");

GlobalVars.globalSettings.InitAndSetArrayItem("", "strFromEmail");


You can add your code to AfterAppInit event that will override hard coded settings with your own ones. You can store SMTP settings either in web.config or in the database. Considering the nature of your application web.config is probably a better option.
Here is the article that explains how you can read settings from web.config:

https://asprunner.com/forums/topic/26135-how-to-find-out-your-current-connection-string/

D
DennisB author 3/14/2019



There are many ways to make it work. Nobody ever asked it, I guess this is not a common setup setup.
SMTP settings are being set in include/appsettings.cs file

GlobalVars.globalSettings.InitAndSetArrayItem(false, "useCustomSMTPSettings");

GlobalVars.globalSettings.InitAndSetArrayItem("", "strSMTPUser");

GlobalVars.globalSettings.InitAndSetArrayItem("localhost", "strSMTPServer");

GlobalVars.globalSettings.InitAndSetArrayItem("25", "strSMTPPort");

GlobalVars.globalSettings.InitAndSetArrayItem("", "strSMTPPassword");

GlobalVars.globalSettings.InitAndSetArrayItem("", "strFromEmail");


You can add your code to AfterAppInit event that will override hard coded settings with your own ones. You can store SMTP settings either in web.config or in the database. Considering the nature of your application web.config is probably a better option.
Here is the article that explains how you can read settings from web.config:

https://asprunner.com/forums/topic/26135-how-to-find-out-your-current-connection-string/


Thank you this may be what I needed. I wonder why these settings are written to the appsettings.cs file but then not used? But I think this may allow me to remove my own mail settings and use the settings from the appsettings files as I had expected.
I appreciate today's response.

admin 3/15/2019

Settings written to appsettings.cs are being used. You need to understand that ASP.NET compiles sources .cs files to DLLs and this is where your code resides. .cs files are not required for deployment but come handy when you need to open project in Visual Studio and do some troubleshooting.

D
DennisB author 3/15/2019



Settings written to appsettings.cs are being used. You need to understand that ASP.NET compiles sources .cs files to DLLs and this is where your code resides. .cs files are not required for deployment but come handy when you need to open project in Visual Studio and do some troubleshooting.


I understand and agree. I also agree with a private email you wrote suggesting that I should put smtp settings in the web.config file, as the database connection info is read from the web.config - so if after deployment, there is a need to change the database settings this could be done in the web.config file. Similarly, this would seem to be a good place for ASPRunner.net to store the smtp settings rather than stored in the compiled .dll file. This means that by your suggestion I have to add event code before app init to read the web.config (which would have been a good place in the first place for ASPRunner to write the smtp info) file to get the smtp settings and use those settings instead of the hard coded smtp values.
Also after some trial and error I had to find out after making the changes you suggested - by way of a link to a website - that I needed to put the web.config file in the source folder so it would not be overwritten on every output.
Thanks for the reply.