Jorge Zuverza
Jorge Zuverza

Reputation: 915

Disable sending emails in asp.net

Is there an easy way to disable sending mails in asp.net? I'm working with webforms but I do not want to send emails while working in my development server. I could comment out every place my system sends an email, but I think there is an easier way to do it.

Thanks

Upvotes: 1

Views: 1958

Answers (4)

toepoke.co.uk
toepoke.co.uk

Reputation: 827

I'd recommend:

  1. Use configSource for environment dependent settings (e.g. have a App_Data\config\dev and App_Data\config\live, etc).
  2. In you dev config file just have an empty SMTP setting, as in -

The emails won't be sent, and no exception thrown (at least 4.5.2 of the .NET framework, which is what I'm using.

Hope this helps.

Upvotes: 0

RandomUs1r
RandomUs1r

Reputation: 4190

The easiest way to do what you're trying to do involves a little bit of architecture:

All the email addresses that the app sends to should be variables in your web.config, at which point it becomes easy to duplicate and comment out the email address string lines and set all the emails to go to you.

Otherwise, you have to either comment out where the email's sending from line by line which across many files becomes very error prone when you push into prod, or your app will time out when it's expecting to send an email while your testing.

You can also use properties: http://msdn.microsoft.com/en-us/library/65zdfbdt%28v=vs.71%29.aspx

Upvotes: 0

MikeSmithDev
MikeSmithDev

Reputation: 15797

Hopefully you centralized your "send email" function. Then it is easy. Check if you are local/debug:

HttpContext.Current.Request.IsLocal || HttpContext.Current.IsDebuggingEnabled

And respond appropriately.

I choose to actually send the email, but I change the to to my email account and add an extra message about who the to, cc, and bcc in the message were (and ensure you clear all those fields out so no real email is sent). That way you can tell if your send email is working as expected -- especially if you are switching out templates, replacing data in the email, etc.

If you didn't centralize your "send email" functions... it may be time to bite the bullet and do it.

Upvotes: 6

ChrisLively
ChrisLively

Reputation: 88074

Generally speaking the easiest way to handle this is in the email configuration piece. Usually it's either a database or web.config setting. What you are looking for is the part that defines the name of the mail server to use for sending messages out.

Upvotes: 0

Related Questions