ANSMTP Developers Center > Email Queuing with MSMQ

Installation and Deployment

You should download the ansmtp installer and install it on your machine at first. If you want to distribute or deploy ansmtp without ansmtp installer, please click here to learn more.

Simple Email Queuing

SaveMailEx method of ANSMTP enables your application to send email by pickup path of IIS SMTP Service. All emails are queued and sent in background. Click here for more detail about IIS SMTP Service.

The following code demonstrates how to send email via IIS SMTP Service Queuing.

[ASP]
Dim oSmtp, pickupPath, arRecipients

Set oSmtp = Server.CreateObject("ANSMTP.OBJ")
pickupPath = "c:\inetpub\mailroot\pickup"

arRecipients = Array( "test@adminsystem.net", _
                      "test1@adminsystem.net", _ 
                      "test2.adminsystem.net" )

oSmtp.FromAddr = "test@emailarchitect.net"
oSmtp.Subject = "test subject" 
oSmtp.BodyText = "test body"

For i = 0 To 2
  oSmtp.ClearRecipient
  oSmtp.AddRecipient arRecipients(i), arRecipients(i), 0 
  If oSmtp.SaveMailEx( pickupPath ) = 0 Then
    Response.Write "Email queued for " & arRecipients(i)
  Else
    Response.Write "SaveMailEx method failed for " & arRecipients(i)
  End If
Next

If there are thousands of recipients, it will take a long time to run this .asp script. To save time, I'll introduce another solution, which is very suitable for newsletter applications.

Queuing with MSMQ

Firstly, ASP/ASP.NET application collects the basic information which include email subject, message body and a SQL QUERY statement for selecting recipients from database. Secondly, those information will be packed as xml string and sent to specified MS Message Queuing. Finally, the background application retrieves this xml string from this Queuing and send all email in background.

How to install MSMQ service?

You can install MSMQ service like this:
"Control Panel->Add/Remove Programs->Windows Components->Message Queuing"

Create a private queuing for Email queuing

After installed MSMQ service, you should create a private queuing like this:
"Control Panel->Administrative Tools->Computer Management->Services and Aplications->Message Queuing->Private Queues" "New->Private Queue" and input "emailRequest" as the queue name.

And you should assign everyone full control permission to this private queue, otherwise ASP/ASP.NET might not send xml to this queue.

How to send xml to MSMQ?

The following code demonstrates how to submit a job to MS Message Queuing.

Sub Submit( subject, body, sqlRecipients )
  Dim strXml
  strXml = "<emailRequest>"
  strXml = strXml & "<subject></subject>"
  strXml = strXml & "<body></body>"
  strXml = strXml & "<recipients></recipients>"
  strXml = strXml & "</emailRequest>"
	 
  dim xmlDoc, xmlNode
  'XMLDOM 3.0 is required, you can download it from msdn.microsoft.com
  set xmlDoc = Server.CreateObject("MSXML2.DOMDocument")
	 
  xmlDoc.loadXML strXml

  set xmlNode = xmlDoc.selectSingleNode( "emailRequest/subject" )
  xmlNode.text = subject
  set xmlNode = xmlDoc.selectSingleNode( "emailRequest/body" )
  xmlNode.text = body
  set xmlNode = xmlDoc.selectSingleNode( "emailRequest/recipients" )
  xmlNode.text = sqlRecipients
  
  Dim queue, message, myq
  set queue = Server.CreateObject("MSMQ.MSMQQueueInfo")
  set message = Server.CreateObject("MSMQ.MSMQMessage")
  'please make sure this queue exists on you machine.
  queue.PathName = ".\private$\emailRequest" 
  set myq = queue.Open( 2, 0 ) 
  message.Body = xmlDoc.xml 
  message.Send myq
End Sub

'Supposed that sqlRecipients = "select name, addr from Recipients",
'then the background application will send the email to all recipients 
'queried by this SQL statement

'And no matter how many recipients are there, 
'asp/asp.net just send one xml into MSMQ.
'therefore, it takes very short time to run this .asp

In fact, the above concept is useful to you to develop many other distribution systems. To learn more about MSMQ, please click here.

Download total sample for this article

Note: This sample includes ASP/ASP.NET client and a Visual Basic background application. Access database is used in this sample, you can immigrate it to SQL server easily. You can even deploy client and background applications on different machines.

queue_msmq.zip

Free Email Support

Not enough? Please contact our technical support team.

Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered User)

Remarks
We usually reply emails in 24hours. The reason for getting no response is likely that your SMTP server bounced our reply. In this case, please try to use another email address to contact us. Your Hotmail or Yahoo email account is recommended.



2001-2008 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map