简体   繁体   English

如何在电子邮件中填充.BCC 字段?

[英]How to populate .BCC field in an email?

I am trying, in MS Access 2013, to grab email addresses from a query pointing to a table and populate the .BCC field in an Outlook email.我正在尝试在 MS Access 2013 中从指向表的查询中获取电子邮件地址,并填充 Outlook 电子邮件中的.BCC字段。

I can populate the .To field with email addresses using .recipients.add .我可以使用.recipients.add使用电子邮件地址填充.To字段。

How can I do the same thing in the .BCC field?我怎样才能在.BCC字段中做同样的事情?
I tried dozens of examples and the closest I got is populating the last email address from the table.我尝试了几十个示例,最接近的是填充表中的最后一个电子邮件地址。

Private Sub Command180_Click()
    Dim rs As DAO.Recordset
    Dim OlApp As Object
    Dim OlMail As Object
    Dim strEmail As String
                 
    Set OlApp = CreateObject("Outlook.Application")
    Set OutMail = OlApp.CreateItem(olMailItem)
                
    Set rs = CurrentDb.OpenRecordset("SELECT POCEmail FROM qDistroActiveEmails")
    With rs
        Do Until .EOF
            strEmail = !PocEmail
        
            With OutMail
                .BCC = strEmail
                .Recipients.Add strEmail
            End With

            rs.MoveNext
        Loop
    End With
    rs.Close
    Set rs = Nothing
    
    OutMail.Display

End Sub

Recipients.Add returns a Recipient object (which your code ignores). Recipients.Add返回一个Recipient对象(您的代码将忽略该对象)。 Set it's Type property to olBCC (3)将其 Type 属性设置为 olBCC (3)

Dim recip as Object
...
With OutMail
  set recip = .Recipients.Add(strEmail)
  recip.Type = 3
End With

The BCC property accepts a string of semi-colon-separated email addresses. BCC 属性接受一串以分号分隔的电子邮件地址。 Also note that this code adds emails to recipients and BCC, which means they will receive duplicate emails.另请注意,此代码将电子邮件添加到收件人和密件抄送,这意味着他们将收到重复的电子邮件。 If you want them to be blind copied only, don't add them to Recipients.如果您只希望它们被盲目复制,请不要将它们添加到收件人中。

Use this instead:改用这个:

Private Sub Command180_Click()
    Dim rs As DAO.Recordset
    Dim OlApp As Object
    Dim OutMail As Object
    Dim strEmail As String
    Dim bccEmails As String

    Set OlApp = CreateObject("Outlook.Application")
    Set OutMail = OlApp.CreateItem(olMailItem)

    Set rs = CurrentDb.OpenRecordset("SELECT Email FROM Emails")
    With rs
        Do Until .EOF
            strEmail = !Email
            ' add email to BCC email list string
            bccEmails = bccEmails & strEmail & ";"

            With OutMail
                .Recipients.Add strEmail
            End With

            rs.MoveNext
        Loop
    End With
    rs.Close
    Set rs = Nothing

    ' set BCC using string of concatenated emails                  
    OutMail.BCC = left(bccEmails, Len(bccEmails) - 1) ' trims trailing semicolon

    OutMail.Display

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM