翻譯|使用教程|編輯:況魚杰|2020-03-10 16:33:27.317|閱讀 349 次
概述:本示例使用單個模板和數據庫發送多封電子郵件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
MailBee Objects是一個組件集合。您可以使用該控件創建并將郵件發送到SMTP服務器并從POP3服務器接收、解析以及刪除電子郵件,還能在IMAP4服務器上管理電子郵件以及文件夾。它同樣支持SSL以及S/MIME以獲得更好的安全性能。MailBee控件能在ASP、VB、C++或任何支持ActiveX技術的語言中使用。
本示例使用單個模板和數據庫發送多封電子郵件。
對于每個消息,從數據庫中獲取的實際值將替換模板中的占位符。因此,所有電子郵件將具有相同的外觀,但仍將是個性化的。本示例代碼使用ADO的Connection對象連接到MS Access數據庫。然后,ADO的Recordset對象被“電子郵件”表中的值填充。該表的每個記錄然后用于生成單個電子郵件。
	該代碼假定數據庫位于C:\ Docs \ simple_merge.mdb文件中,并且“電子郵件”表如下所示:
 
	
 
	
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 " 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:\smtp_log.txt"
objSMTP.ClearLog
' Unlock mailer component
objSMTP.LicenseKey = "put your license key here"
' Specify SMTP server name
objSMTP.ServerName = "smtp.site.com"
' Comment next 3 lines if your SMTP server does not
' require SMTP authentication
objSMTP.AuthMethod = 2
objSMTP.UserName = "your mail account name"
objSMTP.Password = "your mail account password"
' Connect to the server. We use single connection
' for sending all e-mails in the database.
If objSMTP.Connect Then
  ' 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 "
  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 send e-mail
    If Not objSMTP.Send Then
      ' Notify user on sending 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
  ' Disconnect from SMTP server
  objSMTP.Disconnect
Else
  ' Notify user on connection error
  MsgBox "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc
End If
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接下來會還有郵件合并和批量電子郵件-高級的相關教程,多多關注,不要錯過哦!您也可以在評論留下你的經驗和建議。除了本教程,想要了解更多電子郵件相關產品信息的可以點擊此處查看。有更多教程資源等著你!" 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:\smtp_log.txt" objSMTP.ClearLog ' Unlock mailer component objSMTP.LicenseKey = "put your license key here" ' Specify SMTP server name objSMTP.ServerName = "smtp.site.com" ' Comment next 3 lines if your SMTP server does not ' require SMTP authentication objSMTP.AuthMethod = 2 objSMTP.UserName = "your mail account name" objSMTP.Password = "your mail account password" ' Connect to the server. We use single connection ' for sending all e-mails in the database. If objSMTP.Connect Then ' 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 " 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 send e-mail If Not objSMTP.Send Then ' Notify user on sending error Response.Write "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 ' Disconnect from SMTP server objSMTP.Disconnect Else ' Notify user on connection error Response.Write "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc & " " End If %> 
==========================================
如果想要購買正版授權MailBee.NET Objects的朋友,可以聯系
關注慧聚IT微信公眾號 ???,了解產品的最新動態及最新資訊。
	 
 
	
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: