Mail merge and bulk e-mail (Queuing)
' 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
<% ' 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: