【问题标题】:How to check sending email address before sending?发送前如何检查发送电子邮件地址?
【发布时间】:2021-12-30 18:16:10
【问题描述】:

我在 Outlook 中使用多个帐户。如果从我不应该发送的地址发送,我想给出一个警告框。

我有两个永远不应该发送的地址(它们是只接收帐户)。

这个例子几乎就是我要找的。 Example - Checking the "To" address.

我相信字符串比较 (StrComp) 和 Item.SenderEmailAddress 是我需要的。

这是我对单个电子邮件地址 (bad.email@gmail.com) 发出警告的尝试。

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
            
On Error Resume Next
' use lower case for the address
' LCase converts all addresses in the To field to lower case
If StrComp((Item.SenderEmailAddress), "bad.email@gmail.com") Then
    Exit Sub
End If
    
Prompt$ = "You sending this from " & Item.SenderEmailAddress & ". Are you sure you want to send it?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
    Cancel = True
End If

End Sub

理想情况下,我会使用相同的代码检查两个或多个地址。示例中的内容应该可以工作。

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
Select Case LCase(Item.To)
Case "alias@domain.com", "alias2@domain3.com", "alias3@domain3.com"
    Item.Send
Case Else
    Prompt$ = "You are not sending this to " & Item.To & ". Are you sure you want to send the Mail?"
    If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
        Cancel = True
    End If
    
End Select
    
End Sub

另外,我应该将代码放在哪里以确保它不断运行并准备就绪?

【问题讨论】:

  • " SenderEmailAddress "返回一个字符串,表示 Outlook 项目的发件人的电子邮件地址。只读。"未发送的邮件中不存在。
  • 你查看的是发件人地址还是收件人地址?
  • 检查发件人(发件人)地址

标签: vba email outlook


【解决方案1】:

我觉得应该是MailItem.SendUsingAccount Property (Outlook)

它返回或设置一个Account 对象,该对象表示要在其下发送MailItem 的帐户。读/写,另见MailItem.SendUsingAccount Property.

例子

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim Prompt As String
        Prompt = "Are you sure you want to send from 0m3r@Email.com?"

        If Item.SendUsingAccount = "0m3r@Email.com" Then
            If MsgBox(Prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
                Cancel = True
            End If
        End If

End Sub

【讨论】:

  • 这似乎对我也不起作用,我没有得到任何提示。
【解决方案2】:

尝试使用SendUsingAccount - 如前所述,未发送的邮件中不存在SenderEmailAddress

Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Dim sendAddress As String
    Dim prompt As String

    ' Check Send_from name
    sendAddress = Item.SendUsingAccount.SmtpAddress

    Select Case sendAddress
        Case "alias@domain.com", "alias2@domain3.com", "alias3@domain3.com"
            ' send
        Case Else
            prompt = "You are currently sending this email from " & sendAddress & ". Are you sure you want to proceed?"
            If MsgBox(prompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground + vbDefaultButton2, "Check Address") = vbNo Then
                Cancel = True
            End If        
    End Select

End Sub

您应该将此代码粘贴到ThisOutlookSession,在 VBA 编辑器的 Microsoft Outlook 对象下。

【讨论】:

  • 非常感谢!我会试试看。
  • 无论我从哪个帐户发送,它似乎都会发出警告。
  • 检查的是实际的电子邮件地址,还是只检查显示名称?因为每个帐户的显示名称可能相同。
  • 已编辑 - 尝试使用 SmtpAddress 而不是 DisplayName。
  • 那也行不通。它只是像往常一样发送电子邮件,没有任何警告。
猜你喜欢
  • 2016-09-25
  • 2022-12-10
  • 1970-01-01
  • 2016-01-21
  • 2015-06-27
  • 1970-01-01
  • 2012-01-21
  • 2016-03-06
  • 1970-01-01
相关资源
最近更新 更多