Click here to Skip to main content
13,251,998 members (53,531 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


83 bookmarked
Posted 13 Dec 2009

Send 1000s of Emails Without Timeout

, 13 Dec 2009
Rate this:
Please Sign up or sign in to vote.
Now you can send more than 1000 emails with a simple website and a process


I faced problems when sending 1000s of letters to members via a simple webpage, so I came up with a small application that can do this process without making the page timeout or fail sending. My application has the option to send emails via SQL Server or a simple SMTP Server.


We had an application that is used to sending daily newsletters to members. Daily newsletters can be articles, ads, services... But suddenly, the members' number started to increase and at the number research above 10000 email. So the webform that was used to send emails to 1000 members started to timeout and give errors since it was looping for every email, adding options to each member and then sending the letters. So we needed another process that can run along with the same page, without making it timeout or block sending letters. The idea was as follows:

We start by creating the body of the letter that will be sent to members, select the option how to send (SMTP or SQL), then start the process of sending. At the end of the process, we can see the log file that resulted through the sending.

Using the Code

First, I started with configuring the SQL Server SMTP provider.

-- configuring sql smpt email sender
USE msdb

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'MyAccount', @email_address = '', _
	@display_name = 'sender name', @mailserver_name = 'localhost'

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'MyDBMail' 

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'MyDBMail', @account_name = 'MyAccount', @sequence_number = 1

The script is used to set up the SQL SMTP email sending. The following procedure is used to send the letters using the configured provider in SQL.

CREATE PROCEDURE [SendNewsletterMails]
    @Email varchar(max),
    @Body text,
    @Subject varchar(255)

EXEC msdb.dbo.sp_send_dbmail
@body= @Body, 
@subject = @Subject,
@body_format = 'HTML',
@profile_name = 'MyDBMail'


Now my webpage has an option either to choose sending the emails via SMTP or SQL.


After setting the options of the email, parameters are sent to the process that will start running and sending the emails to the clients stored in the database.


At the end of the process, the pop up window will close showing that the process has ended successfully.

The log file will be generated at the end of the process showing:


If we are using the SQL Server option, we can check the history of sending emails from there. To check the history in SQL Server for sending emails, we can select the emails saved in Queue in SQL server:

use msdb

--to get successful sending emails
SELECT * FROM sysmail_allitems where sent_status = 'sent'

--to get successful failed 
SELECT * FROM sysmail_allitems where sent_status = 'failed'

In this way, we can tell the number of emails sent successfully and other failed emails.


  • 13th December, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Suha Mneimneh
Team Leader IDS
Lebanon Lebanon
Adore programming, interested in workflows, SharePoint and silverlight, Entity Framework.
My Blog:

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
gaurav_verma_mca13-Dec-09 4:05
membergaurav_verma_mca13-Dec-09 4:05 
GeneralRe: My vote of 2 Pin
lsj_539-Jan-10 2:53
memberlsj_539-Jan-10 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 13 Dec 2009
Article Copyright 2009 by Suha Mneimneh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid