Link to home
Start Free TrialLog in
Avatar of harris9999
harris9999Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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 
mesid=cint(request.Form("mesid"))
'response.Write("mesid=" & mesid & "<br />")
If IsNumeric(mesid) then
'response.Write("inhere")

	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)
	userid=rs("UserId")
	subject=rs("Subject")
	emailbody=rs("EmailBody")
	origemailbody=emailbody
	emailfrom=rs("EmailFrom")
	emailaddress=rs("EmailAddress")
	textbody=rs("TextBody")
	groupids=rs("GroupIds")
		emailmess=emailmess & "<br />Subject: <br />" & subject
	rs.close
	set rs=nothing
	
	strSQL="UPDATE Messages SET Sent=True, DateSent=#" & getUSdate(now()) & "# WHERE MessageId=" & mesid
	'response.Write(strSQL)
	cn.execute(strSQL)

	cn.close
	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 />"
	else
			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.Subject=subject
		myMail.From=emailaddress
		
		
'	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


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

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

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

			'Response.Flush 
			
		'end if
		rs.movenext
		loop
		
		
		set myMail=nothing

Open in new window

Avatar of pdoelle
pdoelle
Flag of Canada image

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.


Avatar of harris9999

ASKER

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.
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?
ASKER CERTIFIED SOLUTION
Avatar of pdoelle
pdoelle
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial