我有一个想要自动发送电子邮件的 MS Access 2010 数据库。我已经设置了查询,但被 CDO VBA 卡住了。他们的查询称为“qryEmails”,包含以下 4 个字段:
ReturnCode, SalesOrderNumber, Name, EmailAddress
我如何获得访问权限:
- 遍历每条记录并向列出的每个电子邮件地址发送一封电子邮件
- 在每封电子邮件中,有一条消息将包含对前 3 个字段的引用,因此每条消息都显得个性化
- 有一个动态的主题,所以
ReturnCode
领域在每个主题中
起初我一直在尝试小步骤,到目前为止,我收到了 100 封发送到同一地址的电子邮件。这是我的代码(我在不想透露信息的地方使用了 XXX):
Dim rst As ADODB.Recordset
Dim strSQL As String
Dim strEmail As String
Set rst = New ADODB.Recordset
'
strSQL = "[qryEmails]" 'source of recordset
rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'
Do While Not rst.EOF
strEmail = rst.Fields("EmailAddress")
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Your refund is:" '
objMessage.FROM = """SENDER"" <XXX@somewhere.com>"
objMessage.To = rst.Fields("EmailAddress")
objMessage.TextBody = objMessage.TextBody & rst(1)
'==Add fields to email body
'Do While strEmail = rst.Fields("EmailAddress")
'rst.MoveNext
'If rst.EOF Then Exit Do
'Loop
' ========= SMTP server configuration
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "XXX"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
'Send email
objMessage.Send
'Clear variable for next loop
Set objMessage = Nothing
Loop
rst.Close
Set rst = Nothing
知道为什么这会发送 100 封电子邮件吗?到目前为止的查询结果仅返回两个地址用于测试目的。