Click here to Skip to main content
Licence CPOL
First Posted 13 Dec 2009
Views 16,365
Downloads 932
Bookmarked 69 times

Send 1000s of Emails Without Timeout

By | 13 Dec 2009 | Article
Now you can send more than 1000 emails with a simple website and a process
 
Part of The SQL Zone sponsored by
See Also

Introduction

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.

Background

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
GO

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'MyAccount', @email_address = 'sender@provider.com', _
	@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)
)
as

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

GO

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

newsletter_sender

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.

New_Picture__1_.jpg

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:

New_Picture__2_.jpg

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
go

--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.

History

  • 13th December, 2009: Initial post

License

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

Member

Follow on Twitter Follow on Twitter
Adore programming, interested in workflows, SharePoint and silverlight, Entity Framework.
My Blog: http://suhamneimne.wordpress.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralI love it - Voted 5 PinmemberAli Hamdar7:58 13 Dec '09  
GeneralRe: I love it - Voted 5 PinmemberSuha Mneimneh8:02 13 Dec '09  
GeneralRe: I love it - Voted 5 PinmemberDewey15:08 13 Dec '09  
GeneralRe: I love it - Voted 5 PinmemberRavi Bhavnani19:11 13 Dec '09  
GeneralRe: I love it - Voted 5 PinmemberSuha Mneimneh21:35 13 Dec '09  
GeneralMy vote of 2 Pinmembergaurav_verma_mca3:05 13 Dec '09  
GeneralRe: My vote of 2 Pinmemberlsj_531:53 9 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 13 Dec 2009
Article Copyright 2009 by Suha Mneimneh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid