本示例使用单个模板和数据库发送多封电子邮件。
MailBee Objects是一个组件集合。您可以使用该控件创建并将邮件发送到SMTP服务器并从POP3服务器接收、解析以及删除电子邮件,还能在IMAP4服务器上管理电子邮件以及文件夹。它同样支持SSL以及S/MIME以获得更好的安全性能。MailBee控件能在ASP、VB、C++或任何支持ActiveX技术的语言中使用。
最新版MailBee Objects
本示例使用单个模板和数据库发送多封电子邮件。
对于每个消息,从数据库中获取的实际值将替换模板中的占位符。因此,所有电子邮件将具有相同的外观,但仍将是个性化的。本示例代码使用ADO的Connection对象连接到MS Access数据库。然后,ADO的Recordset对象被“电子邮件”表中的值填充。该表的每个记录然后用于生成单个电子邮件。
该代码假定数据库位于C: Docs simple_merge.mdb文件中,并且“电子邮件”表如下所示:
- 注意1:该示例通过单个SMTP连接发送所有电子邮件(这提供了最佳性能)。但是,某些SMTP服务器只允许通过同一连接发送少量电子邮件。
- 注意2:为减少批量发送应用程序的响应时间,您可以告诉MailBee在后台发送电子邮件。
Visual Basic:
' Mailer objectDim objSMTP' ADO Connection objectDim objConn' ADO Recordset objectDim rsEmails' Merge patterns (Body and "To:", all other fields remain' unchanged for all e-mails in the mailing list)Dim strBodyPattern, strToPattern' Define body patternstrBodyPattern = "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 componentSet 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 = TrueobjSMTP.LogFilePath = "C:smtp_log.txt"objSMTP.ClearLog' Unlock mailer componentobjSMTP.LicenseKey = "put your license key here"' Specify SMTP server nameobjSMTP.ServerName = "smtp.site.com"' Comment next 3 lines if your SMTP server does not' require SMTP authenticationobjSMTP.AuthMethod = 2objSMTP.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:Docssimple_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.DisconnectElse ' Notify user on connection error MsgBox "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDescEnd 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 componentSet 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 = TrueobjSMTP.LogFilePath = "C:smtp_log.txt"objSMTP.ClearLog' Unlock mailer componentobjSMTP.LicenseKey = "put your license key here"' Specify SMTP server nameobjSMTP.ServerName = "smtp.site.com"' Comment next 3 lines if your SMTP server does not' require SMTP authenticationobjSMTP.AuthMethod = 2objSMTP.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:Docssimple_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.DisconnectElse ' Notify user on connection error Response.Write "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc & ""End If%>
接下来会还有邮件合并和批量电子邮件-高级的相关教程,多多关注,不要错过哦!您也可以在评论留下你的经验和建议。除了本教程,想要了解更多电子邮件相关产品信息的可以点击此处查看。有更多教程资源等着你!
==========================================
如果想要购买正版授权MailBee.NET Objects的朋友,可以联系在线客服
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!