Check receiver of incoming emails

V

Veerle

Hi,

I would like to create a rule in outlook that moves all the incoming
emails (or emails in my inbox) for which the receiver's emailaddress is
not in a certain list to the spam folder. This is because we have a
domainname veerle-en-koen.be, but only use like 4 emailaddress of this
domainname and we get lots of spam to the other emailaddresses of this
domain like (e-mail address removed), (e-mail address removed), ...
The reason is that the company where we registered our domainname
forwards all the mail of the domain. So I would like to check the
receiver(s) of the incoming mails to see if one of our 4 really used
emailaddresses are in it, and if not: move the mail to the spam folder.

But I don't think this is possible with the rules of outlook. So now
the real question: I could write something in VBA that does this check,
but how can I make sure this VBA script is automaticcaly executed each
time new email arrives? If I could get an extra button on one of the
outlook toolbars that executes the script when pressed and parses all
the emails in the inbox would be fine as well, is that possible?

Veerle
 
E

Eric Legault [MVP - Outlook]

If you wanted to handle this, you'd need to write a VBA macro to trap the
ItemAdd event for the Inbox folder that is referenced by a MAPIFolder object
whose Items collection has been globally declared using With Events in the
ThisOutlookSession module, and set during the Application_Startup event.
However, Outlook will need to be running all the time, and the ItemAdd is not
*guaranteed* to fire if many messages are delivered/copied/moved at once
(usually greater than 16).

If you need a perfect solution that will run on the server side and does not
require Outlook to be running, and that is guaranteed to fire on all incoming
mail, you have to write an Exchange Event Sink. For more info on that, see:

Microsoft Exchange Server Development Technologies:
http://www.outlookcode.com/d/exstech.htm

You can also ping me offline, as I know event sinks very well.

I've included the starter code below to start you on your path. All you
need to do is add the code to check the sender’s e-mail address against
another list and move it to your spam folder if it matches.

Option Explicit
Dim WithEvents NewMailItems As Outlook.Items

Private Sub Application_Startup()
Set NewMailItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub NewMailItems_ItemAdd(ByVal Item As Object)
'THIS WILL FIRE FOR EVERY NEW E-MAIL; YOU CAN USE THE
'Item OBJECT TO WORK WITH THE PROPERTIES OF THE E-MAIL MESSAGE
Dim objMail As Outlook.MailItem
If Item.Class <> olmail Then Exit Sub
Set objMail = Item
'Now you can do someting with the e-mail
End Sub

Private Sub Application_Quit()
Set NewMailItems = Nothing
End Sub
 
R

Reiner Block

I guess it seems that it depends on the version.

I've written at Outlook 2003 on event NewMail, that adds a user field to each new mail which holds the receiving date. But with Outlook 2007 it does not work anymore. I even tried it with ItemAdd but both fails in the case you've rules that moves the new e-mails in other folders.

It seems that Outlook 2007 now first handles the rules before the events of the inbox folder can be fired. :-( Outlook 2003 first fired the event and then executed the rules.

Sighing greetings

Reiner

Eric Legault [MVP - Outlook] erklärte :
If you wanted to handle this, you'd need to write a VBA macro to trap the
ItemAdd event for the Inbox folder that is referenced by a MAPIFolder object
whose Items collection has been globally declared using With Events in the
ThisOutlookSession module, and set during the Application_Startup event.
However, Outlook will need to be running all the time, and the ItemAdd is not
*guaranteed* to fire if many messages are delivered/copied/moved at once
(usually greater than 16).

If you need a perfect solution that will run on the server side and does not
require Outlook to be running, and that is guaranteed to fire on all incoming
mail, you have to write an Exchange Event Sink. For more info on that, see:

Microsoft Exchange Server Development Technologies:
http://www.outlookcode.com/d/exstech.htm

You can also ping me offline, as I know event sinks very well.

I've included the starter code below to start you on your path. All you
need to do is add the code to check the sender’s e-mail address against
another list and move it to your spam folder if it matches.

Option Explicit
Dim WithEvents NewMailItems As Outlook.Items

Private Sub Application_Startup()
Set NewMailItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub NewMailItems_ItemAdd(ByVal Item As Object)
'THIS WILL FIRE FOR EVERY NEW E-MAIL; YOU CAN USE THE
'Item OBJECT TO WORK WITH THE PROPERTIES OF THE E-MAIL MESSAGE
Dim objMail As Outlook.MailItem
If Item.Class <> olmail Then Exit Sub
Set objMail = Item
'Now you can do someting with the e-mail
End Sub

Private Sub Application_Quit()
Set NewMailItems = Nothing
End Sub

--
Eric Legault (Outlook MVP, MCDBA, MCTS: Messaging & Collaboration)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/

--
Reiner
--

"Wer fragt, ist ein Narr für fünf Minuten. Wer nicht fragt, bleibt ein Narr
für immer." Chinesische Weisheit
"Erfolg hat, wer ihm entgegengeht, statt ihm nachzulaufen." Onassis,
Aristoteles
 
E

Eric Legault [MVP - Outlook]

It has never been documented who wins between NewMail events and Outlook
rules, nor should you rely on either to occur in a determined order - for
*all* Outlook versions. If you want to win this battle you need to create an
Exchange Event Sink, which always fires first.

--
Eric Legault (Outlook MVP, MCDBA, MCTS: Messaging & Collaboration)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/


Reiner Block said:
I guess it seems that it depends on the version.

I've written at Outlook 2003 on event NewMail, that adds a user field to each new mail which holds the receiving date. But with Outlook 2007 it does not work anymore. I even tried it with ItemAdd but both fails in the case you've rules that moves the new e-mails in other folders.

It seems that Outlook 2007 now first handles the rules before the events of the inbox folder can be fired. :-( Outlook 2003 first fired the event and then executed the rules.

Sighing greetings

Reiner

Eric Legault [MVP - Outlook] erklärte :
If you wanted to handle this, you'd need to write a VBA macro to trap the
ItemAdd event for the Inbox folder that is referenced by a MAPIFolder object
whose Items collection has been globally declared using With Events in the
ThisOutlookSession module, and set during the Application_Startup event.
However, Outlook will need to be running all the time, and the ItemAdd is not
*guaranteed* to fire if many messages are delivered/copied/moved at once
(usually greater than 16).

If you need a perfect solution that will run on the server side and does not
require Outlook to be running, and that is guaranteed to fire on all incoming
mail, you have to write an Exchange Event Sink. For more info on that, see:

Microsoft Exchange Server Development Technologies:
http://www.outlookcode.com/d/exstech.htm

You can also ping me offline, as I know event sinks very well.

I've included the starter code below to start you on your path. All you
need to do is add the code to check the sender’s e-mail address against
another list and move it to your spam folder if it matches.

Option Explicit
Dim WithEvents NewMailItems As Outlook.Items

Private Sub Application_Startup()
Set NewMailItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub NewMailItems_ItemAdd(ByVal Item As Object)
'THIS WILL FIRE FOR EVERY NEW E-MAIL; YOU CAN USE THE
'Item OBJECT TO WORK WITH THE PROPERTIES OF THE E-MAIL MESSAGE
Dim objMail As Outlook.MailItem
If Item.Class <> olmail Then Exit Sub
Set objMail = Item
'Now you can do someting with the e-mail
End Sub

Private Sub Application_Quit()
Set NewMailItems = Nothing
End Sub

--
Eric Legault (Outlook MVP, MCDBA, MCTS: Messaging & Collaboration)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/

--
Reiner
--

"Wer fragt, ist ein Narr für fünf Minuten. Wer nicht fragt, bleibt ein Narr
für immer." Chinesische Weisheit
"Erfolg hat, wer ihm entgegengeht, statt ihm nachzulaufen." Onassis,
Aristoteles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top