Solved

IIS7 Bulk Emailing - ASP

Posted on 2010-09-23
5
634 Views
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 

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

0
Comment
Question by:harris9999
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:pdoelle
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.


0
 
LVL 3

Author Comment

by:harris9999
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?


0
 
LVL 6

Expert Comment

by:pdoelle
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.
0
 
LVL 3

Author Comment

by:harris9999
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?
0
 
LVL 6

Accepted Solution

by:
pdoelle earned 500 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.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Debug Tools to analyse IIS process: This article focus on taking memory dumps from IIS to determine which code is taking more time and to analyse which calls hangs/causes more CPU usage. To take dumps,download the following. Install1: To st…
Prologue It is often required to host multiple websites on a single instance of IIS, mostly in development environments instead of on production servers. I am sure it is not much a preferred solution on production servers but this is at least a pos…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now