IIS7 Bulk Emailing - ASP

I have created a simple solution for creating html email's and senting to a database of email addresses.  
Created using ASP and hosted on IIS7.

Currently I just loop through the email addresses, creating the email and sending by CDO.
Sometimes if it is going to go to a large number of recipients it would time out.
It could send to up to 4000 emails at once.

Is IIS capable of doing this on its own, or should I get additional software on an asp component or the likes to set up queueing?  

Is there a better way of doing this.  To prevent the script time out etc?

Code is below:
Response.Buffer = True 
'response.Write("mesid=" & mesid & "<br />")
If IsNumeric(mesid) then

	set cn = server.createobject("ADODB.Connection")
	cn.provider = application("strProvider")
	cn.open application("strConMessages")
	strSQL="Select Subject, EmailBody, TextBody, EmailFrom, EmailAddress, GroupIds, UserId From Messages WHERE MessageId=" & mesid
	set rs=cn.execute(strSQL)
		emailmess=emailmess & "<br />Subject: <br />" & subject
	set rs=nothing
	strSQL="UPDATE Messages SET Sent=True, DateSent=#" & getUSdate(now()) & "# WHERE MessageId=" & mesid

	set cn=nothing
	set cn = server.createobject("ADODB.Connection")
	cn.provider = application("strProvider")
	cn.open application("strConContacts")
	If Not IsNull(inArray(Split(groupids,","),0)) then
		strSQL="SELECT DISTINCT Contacts.Email, Contacts.ContactId, Contacts.FirstName, Contacts.LastName, Contacts.Company FROM Contacts Where DoNotContact=False AND Email<>'' AND UserId=" & userid
			emailmess=emailmess & "<br />All Contacts Selected<br />"
			emailmess=emailmess & "<br />Specific Groups Selected<br />"

		strSQL="SELECT DISTINCT Contacts.ContactId, Contacts.FirstName, Contacts.LastName, Contacts.Company, Contacts.Email FROM Contacts INNER JOIN GroupContacts ON Contacts.ContactId = GroupContacts.ContactId WHERE DoNotContact=False AND Email<>'' AND GroupContacts.GroupId IN(0,0" & groupids & ")"
	end if
	set rs=cn.execute(strSQL)
	if not (rs.bof and rs.eof) then
		Set myMail=CreateObject("CDO.Message")
'	myMail.Configuration.Fields.Item _
'	("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
'	'Name or IP of remote SMTP server
'	myMail.Configuration.Fields.Item _
'	("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
'	="localhost"
'	'Server port
'	myMail.Configuration.Fields.Item _
'	("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _
'	=25 
'	myMail.Configuration.Fields.Update

		do until rs.eof
		'if rs("Email")<>"" then
		'response.Write("Name: " & rs("FirstName") & " " & rs("LastName"))
		'response.Write("<br>Email: " & rs("Email"))

		response.Write(k & ",")
            <!--#include virtual="/includes/emailbody.asp" -->

			myMail.TextBody = textbody
			myMail.HTMLBody = emailbody 
			emailmess=emailmess & "<br />Mail Sent To: " & rs("Email")

		'end if
		set myMail=nothing

Open in new window

Who is Participating?

Improve company productivity with a Business Account.Sign Up

pdoelleConnect With a Mentor Commented:
ASP.Net would have similar execution time limitations, but it will work. Using the built-in mail component without specifying an SMTP server will cause the mails to spool on the local print server as you mentioned. The local queue will then proceed to process according to your local SMTP server settings.

There is a short intro to the ASP.Net approach here: http://www.stardeveloper.com/articles/display.html?article=2002112901&page=1

You might also consider having your script triggered periodically using Window's scheduler, and posting a batch each time it is run. This has the added advantage of breaking up your outbound mails so that they are less likely to be construed as spam.

I would expect the script to time out on a very large number of emails; the ASP script timeout can be configured in the IIS Manager under the Application Configuration (Properties > Directory tab > Options tab > Application configuration section.) It might be okay to increase this a bit but I wouldn't crank this up too high. It's there to protect against things like infinite loops killing your server.

If you are content to have your web server churn away sending the emails, you can break them up into batches. Adjust your SQL to page the results (for example, to get only 500 recipients at a time,) send the emails, and then redirect to the same page again, this time passing the record number to start at in the querystring. The script execution time resets each time the page is reloaded.

Paging techniques vary based on the db being used. Some implement the LIMIT clause, making it really easy. Others require a little a little more involved approach (for example: http://www.select-sql.com/mssql/how-to-make-limit-from-to-in-mssql-2005.html).

Remember not to redirect to the page again if you reach the end of your data.

harris9999Author Commented:
Thanks for the feedback.  
The paging looks like an option which would prevent the script time out.

Can I get this to run in the background?  So say from a user point of view, they click on send, that they wouldn't be seeing the redirect all the time which it executes.
When they click send, it could take them to a page which says it is being processed.  Could the send page be called by Server.execute?

For the server churning away at the emails could something like:
http://www.aspemail.com/ be used which would allow the emails to be queued?

What sort of size of emails could be sent with this solution without much problems? E.g. can it take a few thousand ok, e.g. 5000, but say in the case of 50,000 email I assume something more robust would be required?

The paging technique could potentially work for large numbers, but is pretty fragile. Unless you somehow track the successful email recipients/database position, a failure somewhere along the way would require you to start again from the beginning.

A queuing system is a smarter way to go, but requires a daemon/service running in the background on the server to queue and process the emails. ASP scripting, unfortunately, is not well suited to event-driven programming of this nature.

ASPEmail looks good; there is also this: http://www.emailarchitect.net/webapp/smtpcom/.

Neither is free, but it my just turn out to be faster and cheaper (hey, you time *is* worth something) to use a pre-built component. Both have trial versions, so you can see if it will be worth it.
harris9999Author Commented:
Ok have set up the paging for the sending emails.  Seems to work fine but only tested it on small email recipients. Fragile, I suppose there is a high chance of something failing or the server timing out?
Will try the ASPEmail to see how it goes.

I suppose on this page itself it would be possible to set the sending part in ASP.Net?
(Haven't done any of that yet, but will learn at some stage, but could maybe get the mail sending part sorted itself.)
Would ASP.Net need a script a component for sending/queueing or can that manage all that itself? Or would it just fire it all to the server smtp service?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.