Solved

ASP.net async emails

Posted on 2012-04-04
7
551 Views
Last Modified: 2012-08-14
Hello

Can anyone tell me what the performance implications are of sending emails asynchronously in asp.net when there are hundreds getting sent at one time. Is a new thread created for each async email request?

Basically I want to fire and forget emails from my site without waiting on the process to complete, this isn't for bulk email but for individual emails to users when required.

I'm expecting reasonable traffic and I'm just worried that thousands of threads are going to be created and my web server will crawl to a stop - or am I worrying about nothing?

Thanks!
0
Comment
Question by:badpilot
  • 3
  • 2
  • 2
7 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37806711
A new thread would be created for each async call.

Instead, it's better to create one background thread and then send them synchronously from that thread.
0
 

Author Comment

by:badpilot
ID: 37806817
Thanks for your quick reply.

So at Application Start I should create a new background thread and send all mail through that?

Is it a good idea to leave a thread open like this all the time? Do I need to explicitly close this thread at application close? I take it I would need to deal with checking the thread is still live if for some reason it closed do to an exception etc.

Can you provide any code examples or links?

Many thanks!
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37807564
Create the thread when sending the email and then terminate it when the process is done. There is no need to have it open or live the whole time.

Here is an example:
http://www.pcreview.co.uk/forums/send-email-background-thread-t3829532.html
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37808335
Instead of spawning a thread for every email by directly instantiating System.Threading.Thread consider using System.Threading.ThreadPool instead, otherwise you run the risk of starving your system of resources.
Thread Pooling (C# and Visual Basic)
http://msdn.microsoft.com/en-us/library/h4732ks0.aspx
I hope this helps.
0
 

Author Comment

by:badpilot
ID: 37812382
Thread Pooling looks like what I'm looking for - two quick questions if you will:

The sample code on MSDN seems to suggest I don't need to instantiate a threadpool object - I can just add my task direct to the pool - I take it the framework handles everything else for me?

How do I handle errors with tasks added to the pool - do I just need to add a try catch to the sub that I pass to the pool?

Many thanks for you help.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37812487
That's right, .NET handles everything for you so you don't need to instantiate ThreadPool (and since it's a static class it cannot be instantiated).  The callback method should handle errors with try/catch/finally  blocks.
0
 

Author Closing Comment

by:badpilot
ID: 37812495
Perfect, exactly what I needed, thank you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now