IIS7 Bulk Emailing - ASP

Posted on 2010-09-23
Medium Priority
Last Modified: 2012-05-10
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

Question by:harris9999
  • 3
  • 2

Expert Comment

ID: 33750880
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.


Author Comment

ID: 33755327
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?


Expert Comment

ID: 33759395
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.

Author Comment

ID: 33764082
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?

Accepted Solution

pdoelle earned 2000 total points
ID: 33783673
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.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question