inputchip
inputchip

Reputation: 107

How to check sending email address before sending?

I use multiple accounts in Outlook. I want to give a warning box if sending from an address I should not be sending from.

I have two addresses that I should never send from (they are receive only accounts).

This example is almost what I am looking for. Example - Checking the "To" address.

I believe a string comparison (StrComp) and Item.SenderEmailAddress is what I need.

Here is my attempt for giving a warning for a single email address ([email protected]).

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), "[email protected]") 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

Ideally I would to check two or more addresses with the same code. Something like in the example should work.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
Select Case LCase(Item.To)
Case "[email protected]", "[email protected]", "[email protected]"
    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

Also, where do I place the code to ensure that it is constantly running and ready?

Upvotes: 2

Views: 5654

Answers (2)

BigBen
BigBen

Reputation: 50008

Try using SendUsingAccount - as noted, SenderEmailAddress doesn't exist on unsent mail.

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 "[email protected]", "[email protected]", "[email protected]"
            ' 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

You should paste this code in ThisOutlookSession, under Microsoft Outlook Objects in the VBA Editor.

Upvotes: 1

0m3r
0m3r

Reputation: 12499

I think it should be MailItem.SendUsingAccount Property (Outlook)

Which returns or sets an Account object that represents the account under which the MailItem is to be sent. Read/write, also see MailItem.SendUsingAccount Property.

Example

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim Prompt As String
        Prompt = "Are you sure you want to send from [email protected]?"

        If Item.SendUsingAccount = "[email protected]" Then
            If MsgBox(Prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
                Cancel = True
            End If
        End If

End Sub

Upvotes: 1

Related Questions