CDO电子邮件自动化

时间:2013-12-16 13:18:37

标签: email ms-access access-vba ms-access-2010 cdo.message

我有一个MS Access 2010数据库,我想自动发送电子邮件。我已经设置了查询但是卡在了CDO VBA中。它们的查询称为“qryEmails”,包含以下4个字段:

ReturnCode, SalesOrderNumber, Name, EmailAddress

如何获取访问权限:

  1. 遍历每条记录并向列出的每个电子邮件地址发送电子邮件
  2. 在每封电子邮件中,都有一条消息,其中包含对该消息的引用 前3个字段,因此每条消息都显示为个性化
  3. 拥有动态主题,因此每个主题中都有ReturnCode字段
  4. 我一开始就尝试了一些小步骤,到目前为止,我收到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封电子邮件?到目前为止,查询结果只返回两个地址用于测试目的。

1 个答案:

答案 0 :(得分:1)

在循环内,记录集保持在同一行。由于记录集行没有改变,因此它永远不会达到rst.EOF

该代码包含MoveNext的禁用行。取消注释该行。您可能希望在Loop语句之前定位它。

Do While Not rst.EOF
    ' do everything you need for current record,
    ' then move to the next record ...
    rst.MoveNext
Loop