I've seen variations of this question before, but can't find any now so figured I'd ask and answer it.
I wrote this VBa to resolve the issue! Now when I click send, if I'm not sending from a specific account it will look through the 'send to list' and then prompt me to either cancel or continue. This means it's wonderfully non-invasive! If I click cancel (do not send), the email remains open and unchanged.
Open the developer ribbon, open Visual Basic. Open the 'ThisOutlookSession' and paste in the following code
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim oMail As MailItem
Set oMail = Item
Dim shouldSend As Boolean
shouldSend = ShouldSendEmailFromBusinessAccount(oMail)
If Not (shouldSend) Then
MSG1 = MsgBox("Are you sure you want to send this from the account you're using?", vbYesNo, "Are you sure?")
End If
If MSG1 = vbNo Then
Cancel = True
End If
'Cancel = True
End Sub
Private Function ShouldSendEmailFromBusinessAccount(ByVal oMail As MailItem) As Boolean
ShouldSendEmail = True
'Set the recipients domains/email addresses you want to check.
Dim sendToEmails(0 To 2) As String
sendToEmails(0) = "@domain.co.uk" ' block a domain by TLD
sendToEmails(1) = "domiain2" ' block an entire domain
sendToEmails(2) = "person@domain.com" ' block a person
'The only account you want to send emails to
Dim theAccountsToSendEmailsFrom(0 To 0) As String
theAccountsToSendEmailsFrom(0) = "dave@business.co.uk"
Dim recCount As Integer
Dim myRec As Outlook.Recipient
Dim mySender As String
mySender = oMail.SendUsingAccount
For a = 0 To UBound(theAccountsToSendEmailsFrom)
theAccountToSendEmailsFrom = theAccountsToSendEmailsFrom(a) ' note, one is plural
If (InStr(mySender, theAccountToSendEmailsFrom) = 0) Then
recCount = oMail.Recipients.Count
For i = 1 To recCount
Set myRec = oMail.Recipients(i)
myAddress = myRec.Address
For j = 0 To UBound(sendToEmails)
If (InStr(LCase(myAddress), LCase(sendToEmails(j)))) Then
MsgBox ("Ooops, you are going to send to: " & sendToEmails(j) & " from " & mySender)
ShouldSendEmail = False
Exit For
End If
Next
Next
End If
Next
ShouldSendEmailFromBusinessAccount = ShouldSendEmail
End Function
Since I only want to send to the domains from my work account, if I try to send from any other, I get:
![enter image description here](../../I/static/images/7baa8b291878b8acd04f764883ca90cf9b2a88d73cc4f93b71bad2db804bf9ff.png)
Otherwise, it will just send as normal.
The above code will check every outgoing email address! This means it will check the To, CC and BCC... As soon as it finds a single recipient which matches, it will then show the Prompt asking if you want to send or not.
1"theAccountToNotSendEmailsFrom" made me laugh as a name for something – Devon Parsons – 2014-04-10T10:24:04.823
1To be clear, this is checking for any string defined in your
doNotSendToEmails
array to be in any of the recipients' addresses, right? Does this work for multiple recipients as well as cc and bcc? – Raystafarian – 2014-04-10T12:24:03.447At the moment, the above will check for all those in the To field - it does not check for BCC nor CC. Do you want me to update it @Raystafarian? – Dave – 2014-04-10T12:25:11.607
No, I'm not in need of it. I just wasn't sure if the
Outlook.Recipient
object included the othertypes
of recipients. +1 – Raystafarian – 2014-04-10T12:26:55.427@Raystafarian, thank you, I stand happily corrected :) I just tried it with multiple emails... Outlook.Recipient will read each email you enter and it will check the To, CC and Bcc files... I've updated an error in the code as well so it now checks each recipient individually – Dave – 2014-04-10T12:50:36.873