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 7647941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:35:14+00:00 2026-05-31T10:35:14+00:00

I have built a bulk email sending website for a client that is required

  • 0

I have built a bulk email sending website for a client that is required to send out 80,000 emails in a single send. It basically creates a new thread for the send so that control can be handed back to the UI (so that a feedback page can load) and then a new thread is created for each company in order to send emails to their recipients. The emails are all queued up using this code:

// Loop through the companies and send their mail to the specified recipients
        // while creating a new thread for each company
        // A new thread is started so that the feedback page can load
        SendingThread = Task.Factory.StartNew(() =>
        {
            // This is a thread safe for loop
            Parallel.ForEach<CompanyEntity>(companies, company =>
            {
                    // Start a new thread for each company send
                    Task.Factory.StartNew(() =>
                    {
                        // Get the recipients for this company
                        var companyRecipients = GetSubscribersForCompany(company.Id, recipients);

                        // Send the newsletter to the company recipients
                        var success = SendNewsletterForCompany(newsletter, company, companyRecipients, language,
                                                               version, company.AdvertCollectionViaNewsletterCompanyAdvertLink, newsletter.NewsletterType, email);

                        // Add the status update so the front end can view a list of updated conpany statuses
                        if (success)
                            AddStatusUpdate(company.CompanyTitle + " has completed processing.");

                        // Starts sending the emails if the engine hasn't already been started
                        SendEngine.Start(CurrentSmtpClient, this);

                    }).ContinueWith(antecendent => EndCompaniesSendUpdate(companiesToProcess, companiesProcessed), TaskContinuationOptions.OnlyOnRanToCompletion);
            });
        }, new CancellationToken(), TaskCreationOptions.LongRunning, TaskScheduler.Default);

While the emails are queued, the send engine takes over and pulls emails from the queue and then sends them using the new Parallel class:

Action action = () =>
        {
            MailMessage message;
            while (queue.TryDequeue(out message))
            {
                SendMessage(sendingServer, message, factory);
            }
        };

        // Start 5 concurrent actions to send the messages in parallel.
        Parallel.Invoke(action, action, action, action, action);

All of this works great and can send 40,000 newsletters out in about 10 minutes. The only problem is that the RAM and CPU on the server are 100% consumed for those 10 minutes. This affects other sites on the server as they can’t be accessed.

Is there any way to restrict the resource usage of the sending application either in IIS 7.5 or by changing the code above?

  • 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-31T10:35:16+00:00Added an answer on May 31, 2026 at 10:35 am

    Problems:

    • You are generating a thread inside of a Parallel ForEach, The “Parallel” part means it’s already spawning a thread for the body. You are nesting Parallel Invoke instead of an Action inside of a Parallel ForEach inside of another Action.

    • You are running a while loop inside of a thread with no rest for the CPU. That is being Parallel Invoked 5x.

    Answers:

    For CPU usage, you need to give your processing a breather. In your While TryDequeue loop put a short Sleep.

            MailMessage message;
            while (queue.TryDequeue(out message))
            {
                SendMessage(sendingServer, message, factory);
                Thread.Sleep(16);
            }
    

    For RAM and CPU usage, you need to process LESS at once.

            SendingThread = Task.Factory.StartNew(() =>
            {
                foreach(var company in companies) 
                {
                            // Get the recipients for this company
                            var companyRecipients = GetSubscribersForCompany(company.Id, recipients);
    
                            // Send the newsletter to the company recipients
                            var success = SendNewsletterForCompany(newsletter, company, companyRecipients, language,
                                                                   version, company.AdvertCollectionViaNewsletterCompanyAdvertLink, newsletter.NewsletterType, email);
    
                            // Add the status update so the front end can view a list of updated conpany statuses
                            if (success)
                                AddStatusUpdate(company.CompanyTitle + " has completed processing.");
    
                            // Starts sending the emails if the engine hasn't already been started
                            SendEngine.Start(CurrentSmtpClient, this);
    
    
                }
           }, new CancellationToken(), TaskCreationOptions.LongRunning, TaskScheduler.Default);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have built a web application that accepts SOAP messages, does some processing, calls
I have built an app that plays lots of sounds the easy way: AudioServicesPlaySystemSound(someSoundID);
I have built a MVC website on IIS6. I used the built-in ASP.NET Security
I have built a website and when my users load up http://info.salemgolfclub.org/Account/Logon the username
I have a CPP source file that uses #if / #endif to compile out
I have a table that prints out all available cameras. It uses a form
I have a list of around 5,000 to 10,000 (individual user supplied) email addresses
I have built a simple menu in jQuery http://vanquish.websitewelcome.com/~toberua/ Here is a sample of
I have built UI, its like a search engine for BioProcess/Disease--> Genes. e.g., User
I have built Web sites with Python/Django and desktop applications with Objective-C/Cocoa so programming

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.