邮件组件集合MailBee Objects发送电子邮件教程(七):邮件合并和批量电子邮件-高级

本示例使用单一模板和数据库发送多封带附件的邮件。与简单版示例的两个主要区别。

MailBee Objects是一个组件集合。您可以使用该控件创建并将邮件发送到SMTP服务器并从POP3服务器接收、解析以及删除电子邮件,还能在IMAP4服务器上管理电子邮件以及文件夹。它同样支持SSL以及S/MIME以获得更好的安全性能。MailBee控件能在ASP、VB、C++或任何支持ActiveX技术的语言中使用。

最新版MailBee Objects


本示例使用单一模板和数据库发送多封带附件的邮件。与简单版示例的两个主要区别。

  • 支持带有附件等可选部分的邮件。例如,有的用户会收到附件,有的用户不会收到。
  • 限制通过单次连接会话发送电子邮件的数量。当您的SMTP服务器施加这种限制时,该功能非常有用。

该代码假设数据库位于C:Docs/advanced_merge.mdb文件中,”email “表如下:

id email first_name last_name filename
1 account@domain.com John Doe 345.doc
2 bill.smith@company.com Bill  Smith
3 Kate@site.com Kate Jones 377.doc

注意:某些SMTP服务器限制每秒(而非每个连接)的最大发送数量。例如,您每秒只能从同一IP地址发送5封电子邮件。在这种情况下,您可以在MailBee Message Queue系统中使用排队和后台传递。有关详细信息,请参见排队示例。

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' Counter to limit number of sends per connectionDim I' 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 = "localhost"' 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"' Create ADO Connection objectSet objConn  = CreateObject("ADODB.Connection")' Connect to Access databaseobjConn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _   "Data Source=C:Docsadvanced_merge.mdb;"' Create ADO Recordset objectSet rsEmails = CreateObject("ADODB.Recordset") ' Open "emails" table. This table is' actual data source for the mergersEmails.Open "emails", objConn' Initialize counter to connect to SMTP' server before sending any e-mailsI = 1' Loop through the tableDo While Not rsEmails.EOF  ' Connect to the server. We connect only one time  ' per 10 sends and reuse existing connection for  ' subsequent 9 e-mails.  If I = 1 Then    If Not objSMTP.Connect Then      ' Notify user on connection error and exit      MsgBox "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc      Exit Do    End If  End If  ' Set all properties for each e-mail.  ' This is required because we will reset  ' the message later to free Attachments  ' collection.  ' 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"   ' 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 body 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"))   ' Add attachment if required  If rsEmails("filename") <> "" Then    objSMTP.Message.AddAttachment "C:Data" & rsEmails(""filename"")  End If  ' Try to send e-mail  If Not objSMTP.Send Then    ' Notify user on sending error    MsgBox ""Error #"" & objSMTP.ErrCode & ""

                                                        

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年11月4日
下一篇 2020年11月4日

相关推荐

发表回复

登录后才能评论