'Restrict only works for some recipients
I have some code to keep my inbox clean.
Process:
- Manually mall mails from outbox to inbox. Now I have all mails to be archived in inbox.
- Run code.
When I had a pop3/*pst-files
setup, I used Outlook rules and had some code to run all rules on inbox.
When changing setup to Exchange, problems with rules arrived.
Suddenly I had many duplicates of rules, Outlook could not sync to server etc.
Now I have a simple and effective concept where a rule looks like this:
@somedomain.dk|XYZ
The rule says: Move all emails to/from sender on @somedomain.dk
to Exchange-folder XYZ
.
I select mails to be moved with this line:
Set mItems = inbox.Items.Restrict(strQry)
iNo = mItems.count
The query (it works):
@SQL="urn:schemas:httpmail:displayto" like '%somedomain.dk%' OR
"urn:schemas:httpmail:fromemail" like '%somedomain.dk%'
In the inbox I have 7 mails: Received 4 from and sent 3 to this domain. When running the code 5 mails are collected. Of the 3 emails I have sent, 1 is created by me and 2 are replies. Using Outlook Spy I find that MailItem.To holds both name and email address in the mail I have created, but only name when replying.
How can I change the query so I get the email address? I have looked around in Outlook Spy but got lost.
Solution 1:[1]
This solution do what I need: Move emails to/from somecustomer.com to customer folder.
Email From: I found that email.SenderEmailAddress does not catch all mails. Some are instead found with email.Sender.Address.
Email To: A litte hard nut to crack. Email.To finds some but only if emailadress is written with name (Firstname Lastname <[email protected]>). I found that the smtp address property can find the rest.
(Module header)
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
'>> In procedure
(set objects etc...)
strFrom1 = email.SenderEmailAddress
strFrom2 = email.Sender.Address
strTo1 = email.To
If strTo1 <> "" Then
strTo2 = email.Recipients(1).PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
Else
strTo2 = ""
End If
(loop mails and rules...)
strDom = "*" & somedomain.com
'>> Test if mail is to/from somedomain.com
If strFrom2 Like strDom Or strTo1 Like strDom Or strTo2 Like strDom Then
(do stuff: Move, delete)
End If
(do the rest)
Speed concerns: Processing one mail at a time takes a little long that using restrict but: I did not find a way to move multiple mails in one command, so the moving is done one at a time anyway.
In my test there is no difference in time to move 1-200 mails.
Solution 2:[2]
Outlook Object Model won't let you create subrestrictions on recipients or attachments even though MAPI has no problem doing so.
You have a couple of options -
- Modify recipient names to include the address as well as name (To/CC/BCC properties are rebuilt automatically by the store provider when the message is saved based on the recipient table contents). OOM, again, won't help you -
Recipient.Name
is read-only - If using Redemption (I am its author) is an option, its version of Items.
Find
and Items.Restrict
create a subrestriction on recipient name/address/SMTP address if you use Recipients/To/CC/BCC properties in the query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |