Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:34:44+00:00 2026-05-31T09:34:44+00:00

In continuation with my problem here , I created a test app that uses

  • 0

In continuation with my problem here, I created a test app that uses ThreadPool threads to send emails. Here’s the code I am using.

        protected void btnSend_Click(object sender, EventArgs e)
        {
            EmailInfo em = new EmailInfo { Body = "Test", FromEmail = "test@test.com", Subject = "Test Email", To = "test@test.com" };
//txtNumEmails is a textbox where I can enter number of emails to send
            for (int i = 0; i < Convert.ToInt32(this.txtNumEmails.Text); i++)
            {
                bool bResult = ThreadPool.QueueUserWorkItem(new WaitCallback(EmailNow), em);
            }

        }


        public void EmailNow(object emailInfo) // Email Info object
        {
            EmailInfo em = emailInfo as EmailInfo;

            SmtpClient client = new SmtpClient("localhost");
            client.UseDefaultCredentials = true; 

            client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
            client.PickupDirectoryLocation = @"C:\temp\testemails\";

            MailMessage m = new MailMessage();
            m.To.Add(new MailAddress(em.To));
            m.Body = em.Body;
            m.IsBodyHtml = false;
            m.From = new MailAddress(em.FromEmail);
            m.Subject = em.Subject;

            client.Send(m);
        }

For smaller numbers (10k, 50k) they work great, but once I increase the number to 200k (which is my target), I got this exception:

enter image description here

It created about 186k emails before it threw this exception.

I am assuming this is not the exception caused due to lack of disk space (since i am storing all the emails locally in C:\temp\testemails but because it was low in RAM(?). As suggested by a this user, I used semaphores to limit them to 10k, but still ran in to the same issue. Here’s the code for the one with semaphores.

    protected void Button1_Click(object sender, EventArgs e)
    {
        EmailInfo em = new EmailInfo { Body = "Test", FromEmail = "test@test.com", Subject = "Test Email", To = "test@test.com" };
        var semaphore = new SemaphoreSlim(10000, 10000);

        for (int i = 0; i < Convert.ToInt32(this.txtNumEmails.Text); i++)
        {
            semaphore.Wait();
            bool bResult = ThreadPool.QueueUserWorkItem( (state) => { 
                try 
                {           
                    EmailNow(em);
                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {
                             semaphore.Release();
                }
            },  null);
        }
    }

This one threw an exception as well but I see all 200k emails in the folder. I can definitely use try catch and gracefully exit if this exception occurs, but how would I prevent this from happening in the first place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:34:45+00:00Added an answer on May 31, 2026 at 9:34 am

    Try reducing the 10000 to 1000 for the SemaphoreSlim constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation of the question here: JBoss - does app have to
I've created a form that is used to calculate monthly expenses. The problem I'm
Design problem- one function calls all three I continuation from another question after that
This is in continuation with the question posted here: Finding the center of mass
I'm building an application that works with the db of another application, using mssql
This is a continuation on this question : The problem is simple. I need
I've my working on using the TPL in C# (.NET 4.0). I have created
This is a continuation of my last question . In that question I showed
This is a continuation of my question Python2.5.2 The code i developed is working
I have thing problem here is the debugging outputs ?uƒn74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0 should be ?u=83n74tn5187r&key=6e6e0936c4e6c48be56a72eba8964df0 I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.