I would like to send email differently on production then the development environment, such as sending emails only to me while testing. I know that I can set an application value in the webconfig file or check the url before sending the emails. I would like to know what is the best practice for doing this or if there is some other option that I don't know about that might be better? Any information on this topic would be greatly appreciated.
5 Answers
I use what's called Preprocessor Directives (in C#). In VB it's called Directives only.
I have a helper method like this:
public static bool IsDebug()
{
#if DEBUG
return true;
#else
return false;
#endif
}
Then anywhere in the code one can call this method and during runtime and it'll tell if the code is being run in Debug
( development ) or if it's the Release
version ( production ).
Here's the equivalent #If...Then...#Else Directives in (VB).

- 100,159
- 46
- 371
- 480
-
2What if it is being run in release mode, but its staging so we still want to exclude this functionality there? – JsonStatham Feb 27 '20 at 15:58
-
1@JsonStatham You want your staging to be as close to production as possible so having just prod/dev and everything else being in configuration is way to go having staging as separate environment makes it kind of useless if you think about it. You cannot make any assumptions about your prod since the first time its run is well.. in production. Basically staging is just bit differently configured production, usually with disabled or mocked 3rd party services so you are not triggering alerts, sending emails and so on otherwise it should be the same code as prod. – Hnus Jun 30 '22 at 15:02
If you want to only send the email to yourself if it's not Production then do it by changing the email address on the "To" field.
So either change it in the database or config file, wherever you put that value.
The way the application determines if you are in the development server is with the "ASPNETCORE_ENVIRONMENT".
You can check this link, Use multiple environments in ASP.NET Core

- 943
- 1
- 15
- 25
Well, only you can determine what's production and what's development, so a flag in web.config is ok, but a more elegant solution would involve a pattern like Dependency Injection (DI) ala Spring.Net, StructureMap, and Castle Windsor to name a few.
The key concept behind DI is decoupling/isolating variations in an implementation by dynamically providing information about what class to instantiate at runtime. In this case, for instance, all your code would be written to deal with a 'mail' interface (oblivious to whether it's prod or dev environment) and you'd configure your DI framework to instantiate either the production or the development version of a mail implementation class. It's a bit cleaner in that you're not exposing and proliferating config parameter values.
There's certainly a learning curve there though, so it may not be viable at this stage of your project, but definitely something to keep in mind.

- 23,344
- 7
- 42
- 67
You can use web config inheritance of IIS to allow your app to 'inherit' global context settings from the respective environment into which it is deployed.
In a parent directory of where your .NET app is installed (e.g. /inetpub/wwwroot.web.config
, or even the root web.config ( \Windows\Microsoft.NET\Framework\ver\web.config
), add your environment specific settings, e.g.
In PROD
<appSettings>
<add key="sendMailTo" value="" />
<add key="environmentName" value="PROD" />
</appSettings>
In DEV
<appSettings>
<add key="sendMailTo" value="me@mydomain.com" />
<add key="environmentName" value="DEV" />
</appSettings>
These environment context settings aren't deployed everytime you redeploy your app. Also, all other apps you deploy to your servers can also inherit these settings.

- 104,537
- 17
- 209
- 285
-
Is there any performance cost with checking web config values throughout the site? – user1572948 Aug 27 '12 at 06:44
-
@user1572948 AppSettings are read once and cached, however further performance enhancements are apparantly possible http://stackoverflow.com/questions/1304897/asp-net-web-config-appsettings-performance – StuartLC Aug 27 '12 at 06:52
-
Thanks for the info, if theyre read once and cached it shouldn't be an issue for me then – user1572948 Aug 27 '12 at 07:00
Instead of changing your code, why don't you set your development mail server to only mail to you regardless of the TO address field?
Seems better than writing code to do that.

- 29,284
- 24
- 107
- 141
-
Thanks, yea I could set up the smtp in the IIS. I think I could also set it up to store emails in a folder instead of sending them. – user1572948 Aug 27 '12 at 06:55