There is a couple of ways to debug software in C# Microsoft SharePoint which sends out emails – without actually sending out those emails to users. For updates with SharePoint 2019 please see our Local Aberdeen & Edinburgh page.
Adding a simple #IF DEBUG line around the line of code which actually sends the email will allow you to check that is configures everything correctly, but without the need to post out as this code gets skipped when Visual Studio is set up in debug mode.
The code is now in release mode on a fully configured server – so will start sending emails to users filled with test data.
A better option is to add a variable to your web.config which details the environment we are in; dev, test or prod. Once this is in place we can set up options for each in a switch statement. In production we should actually send the email, in test we could send the email but only to one specific user; and in dev we could log the whole email to the ULS logs for inspection later on.
Now you can step through your code, or let it run knowing that emails won’t be leaking from your development site.