Forum Moderators: open

Message Too Old, No Replies

Sending emails via ASP

Can i send 1 per second?

         

webboy1

11:44 am on Aug 5, 2003 (gmt 0)

10+ Year Member



Hi,

I have a mailing list script which we send out to our subscribers every quarter. (its not spam. We are very strict on how we get our users to request more information.....but that is another conversation).

As it stands just now, i send these letters out at around 300-400 every 5 minutes. Then send the next 300-400 and so on.

I don't like the idea of instantly clogging the server with several thousand outgoing emails. My ideal situation would be to be able to run this script so that it sent 1 email every 2-3 seconds, allowing the server time to send and not clogg up.

Is this possible with ASP and SQL? Below is the script i currently use:
=========================================
Set cn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.RecordSet")

cn.ConnectionString = connstr
cn.Open

rs.ActiveConnection = cn
rs.Open "SELECT * from my_table where id BETWEEN 100 and 500 and emails_active = 1 ORDER by id "
%>


<%
While Not rs.EOF
Set mail = Server.CreateObject("CDONTS.NewMail")
mail.From = "something@ourwebadress.com"
mail.To = rs("email")
mail.Subject = "News from our team"
mail.Body = message & rs("email")
mail.Send
Set mail = Nothing%>

<%
'This part shows a count on screen of all the email that have been sent.
no = no + 1%>

<%Response.write(no)%>
<%Response.write " - "%>
<%=rs("id")%>
<%response.write "<br>"%>

<%Response.Flush%>

<%
rs.MoveNext
wend%>
======================================

Basically just now, all i do is send the above. Then update the SQL line to pull the next 300-400 or so, re-upload the page and send again.

It does work, but as you can imagine, its a long process.....especially if there are several 1000 users registered.

Any ideas on howi can make this more efficient......and automatic i.e. hit run once and they all send at around 1 email every few seconds?

Regards
Webboy

aspdaddy

2:23 pm on Aug 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is really necessary to create and destroy the mail item each time in the loop? This might be creating more overhead than the sending.

Why not use the BCC feature and just send 1 email.

If this isnt possible - I would send them all at once in the script (there only actually going as far as Exchange) , and then look at scheduling/managing the bulk sending from Exchange.

gangstah

11:54 pm on Aug 5, 2003 (gmt 0)

10+ Year Member



I know SMTPMail from SoftArtisans has a MassMail method where you can basically bind your AddRecipient method to your recordset fields that contain the user's name and email address. It's $129US but it's a great tool.

asmith_2048

1:29 am on Aug 6, 2003 (gmt 0)

10+ Year Member



1. aspdaddy makes a good point - it is NOT necessary (or desirable) to recreate the mail component each time. Memory allocation is the most expensive operation you can carry out (for a start you guarantee a CPU cache miss), so you should only do it when you have to. Do your create before the While, and your destroy after the Wend.

2. (I will get to your question eventually =) It is also not necessary to use both a connection object and a recordset in script - use recordset.open only, passing the connection string as the second parameter (I assume you're happy with a read-only, forward only cursor - more about that in point 4). The reason for this is ADO will create the connection object for you, and it will do it more efficiently than the VBScript - I won't bore you with the details.

3. My understanding is there is no "Sleep" method in VBScript (there isn't one in VB6). I assume this is because the designers never considered that a developer would WANT to slow down an executing web script. If you have determined that the other suggestions in this thread are not suitable for your circumstances, you can build a trivial COM component that will expose the Win32 Sleep API call to your script. You can do this in VC++ directly. If you have VB you need to supply the unit with a "declare" statement giving it access to the API call. This is not hard and I can give you the source to do it if you're interested. Then what you do is create your "sleeper" object outside the loop, and after each mail.Send, call it with a parameter of 1 or 2 or 3 seconds, whatever. You'll have to set your Script.Timeout to a very large number (number of emails to send * number of seconds to wait between them * some padding for luck) or the script will crash.

4. Finally, (assuming you're going with point 3) you need to consider the impact on your database. A read-only/forward-only cursor is the fastest way to access data, but the cursor is _server-side_, meaning you'll be hogging a database connection for the entire time your script runs. This could get ugly, especially if there is lots of other database activity on your site. I would suggest you use a "static" cursor, which frees up the database connection once the recordset has been returned to the client. You need to set your connection parameters to adUseClient and adOpenStatic, and don't specify a lock type.

asmith_2048

7:12 am on Aug 6, 2003 (gmt 0)

10+ Year Member



Thanks to a freaking pile of documentation I'm studiously avoiding, there's an ActiveX DLL, VB6 source and ASP test script supporting the Win32 API Sleep() method in server-side script waiting for anyone who's interested. This has been tested on IIS 5.0 Win2K SP4. Sticky mail me with your email address and I'll send it to you. All you have to do is leave the copyright and authorship notice in the source.

Please note that if, as aspdaddy assumes, you are connected to an Exchange server over which you, or one of your colleagues has administrative rights (i.e. not using the local SMTP server with the "Smart Host" pointing at a mail server out of your control), this stuff is not necessary.

gangstah

7:26 pm on Aug 6, 2003 (gmt 0)

10+ Year Member



Oops...sorry...missed the

I don't like the idea of instantly clogging the server with several thousand outgoing emails.

so forget my massmail idea.