Mail merge and bulk e-mail (Queuing)

Visual Basic Code | ASP Code

The sample queues e-mail sends for background delivery with MailBee Message Queue or MailBee.NET Queue system.

The code repeats Simple version of the sample (including template and database usage) with only difference that e-mails are queued to MailBee Message Queue system and no actual connection to the SMTP server is made.

SMTP.SendToQueue is a key method of the sample.

When using queing, your application only creates messages (with MailBee, this is extremely fast process). Actual delivery is performed by MailBee Message Queue system (or MailBee.NET Queue, there is no difference for this sample). This allows your application to respond very quickly even if thousands of e-mails have been created and placed into the queue.

Queing is especially useful when the machine running MailBee and actual SMTP server are not within the same local network (and communication between them is relatively slow). Another case is when SMTP server limits rate of e-mail sends from one IP address.

Note: In order to run this sample, MailBee Message Queue or MailBee.NET Queue must be installed and configured properly.

[Visual Basic]:

' Mailer object
Dim objSMTP

' ADO Connection object
Dim objConn

' ADO Recordset object
Dim rsEmails

' Merge patterns (Body and "To:", all other fields remain
' unchanged for all e-mails in the mailing list)
Dim strBodyPattern, strToPattern

' Define body pattern
strBodyPattern = "Hello %%FIRST_NAME%%, " & vbCrLf & _
  "You are welcome to visit our site at:" & vbCrLf & _
  "http://www.site.com" & vbCrLf & vbCrLf & _
  "Sincerely, Site Team"

' Define "To:" pattern (will look like
' "John Doe <j.doe@domain.com>" in e-mail)
strToPattern = "%%FIRST_NAME%% %%LAST_NAME%% <%%EMAIL%%>"

' Create SMTP mailer component
Set objSMTP  = CreateObject("MailBee.SMTP")

' Enable logging SMTP session into a file. If any
' errors occur, the log can be used to investigate
' the problem.
objSMTP.EnableLogging = True
objSMTP.LogFilePath = "C:\Temp\smtp_log.txt"
objSMTP.ClearLog

' Unlock mailer component
objSMTP.LicenseKey = "put your license key here"

' Plain-text e-mail. Change to 1 to send HTML e-mail
objSMTP.BodyFormat = 0
  
' Specify "From:" and "Subject:". They remain unchanged
' for all the e-mails.
objSMTP.Message.FromAddr = "Site Team <sales@site.com>"
objSMTP.Message.Subject = "Invitation"

' Create ADO Connection object
Set objConn  = CreateObject("ADODB.Connection")

' Connect to Access database
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
  "Data Source=C:\Docs\simple_merge.mdb;"

' Create ADO Recordset object
Set rsEmails = CreateObject("ADODB.Recordset")
  
' Open "emails" table. This table is
' actual data source for the merge
rsEmails.Open "emails", objConn
  
' Loop through the table
While Not rsEmails.EOF

  ' Merge body pattern and actual data
  ' from the database
  objSMTP.Message.BodyText = strBodyPattern
  objSMTP.Message.BodyText = Replace(objSMTP.Message.BodyText, "%%FIRST_NAME%%", rsEmails("first_name"))

  ' Merge "To:" pattern and actual data
  ' from the database
  objSMTP.Message.ToAddr = strToPattern
  objSMTP.Message.ToAddr = Replace(objSMTP.Message.ToAddr, "%%FIRST_NAME%%", rsEmails("first_name"))
  objSMTP.Message.ToAddr = Replace(objSMTP.Message.ToAddr, "%%LAST_NAME%%", rsEmails("last_name"))
  objSMTP.Message.ToAddr = Replace(objSMTP.Message.ToAddr, "%%EMAIL%%", rsEmails("email"))

  ' Try to queue e-mail in MailBee Message Queue pickup folder
  ' (this usually succeeds unless disk is full)
  If Not objSMTP.SendToQueue("C:\MMQ Files") Then
    ' Notify user on queuing error
    MsgBox "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc
  End If
    
  ' Proceed with the next record in the table
  rsEmails.MoveNext
Wend
  
' Close the table and the whole database
rsEmails.Close
objConn.Close

' Free database-related objects
Set rsEmails = Nothing
Set objConn = Nothing

[ASP]:

<%
' Mailer object
Dim objSMTP

' ADO Connection object
Dim objConn

' ADO Recordset object
Dim rsEmails

' Merge patterns (Body and "To:", all other fields remain
' unchanged for all e-mails in the mailing list)
Dim strBodyPattern, strToPattern

' Define body pattern
strBodyPattern = "Hello %%FIRST_NAME%%, " & vbCrLf & _
  "You are welcome to visit our site at:" & vbCrLf & _
  "http://www.site.com" & vbCrLf & vbCrLf & _
  "Sincerely, Site Team"

' Define "To:" pattern (will look like
' "John Doe <j.doe@domain.com>" in e-mail)
strToPattern = "%%FIRST_NAME%% %%LAST_NAME%% <%%EMAIL%%>"

' Create SMTP mailer component
Set objSMTP  = Server.CreateObject("MailBee.SMTP")

' Enable logging SMTP session into a file. If any
' errors occur, the log can be used to investigate
' the problem.
objSMTP.EnableLogging = True
objSMTP.LogFilePath = "C:\Temp\smtp_log.txt"
objSMTP.ClearLog

' Unlock mailer component
objSMTP.LicenseKey = "put your license key here"

' Plain-text e-mail. Change to 1 to send HTML e-mail
objSMTP.BodyFormat = 0
  
' Specify "From:" and "Subject:". They remain unchanged
' for all the e-mails.
objSMTP.Message.FromAddr = "Site Team <sales@site.com>"
objSMTP.Message.Subject = "Invitation"

' Create ADO Connection object
Set objConn  = Server.CreateObject("ADODB.Connection")

' Connect to Access database
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
  "Data Source=C:\Docs\simple_merge.mdb;"

' Create ADO Recordset object
Set rsEmails = Server.CreateObject("ADODB.Recordset")
  
' Open "emails" table. This table is
' actual data source for the merge
rsEmails.Open "emails", objConn
  
' Loop through the table
While Not rsEmails.EOF

  ' Merge body pattern and actual data
  ' from the database
  objSMTP.Message.BodyText = strBodyPattern
  objSMTP.Message.BodyText = Replace(objSMTP.Message.BodyText, "%%FIRST_NAME%%", rsEmails("first_name"))

  ' Merge "To:" pattern and actual data
  ' from the database
  objSMTP.Message.ToAddr = strToPattern
  objSMTP.Message.ToAddr = Replace(objSMTP.Message.ToAddr, "%%FIRST_NAME%%", rsEmails("first_name"))
  objSMTP.Message.ToAddr = Replace(objSMTP.Message.ToAddr, "%%LAST_NAME%%", rsEmails("last_name"))
  objSMTP.Message.ToAddr = Replace(objSMTP.Message.ToAddr, "%%EMAIL%%", rsEmails("email"))

  ' Try to queue e-mail in MailBee Message Queue pickup folder
  ' (this usually succeeds unless disk is full)
  If Not objSMTP.SendToQueue("C:\MMQ Files") Then
    ' Notify user on queuing error
    Response.Write "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc & "<br>"
  End If
    
  ' Proceed with the next record in the table
  rsEmails.MoveNext
Wend
  
' Close the table and the whole database
rsEmails.Close
objConn.Close

' Free database-related objects
Set rsEmails = Nothing
Set objConn = Nothing
%>
See Also:
SendToQueue Method
SendToQueueEx Method

 


Send feedback to AfterLogic
Copyright © 2002-2024, AfterLogic Corporation. All rights reserved.