Outlook MailItem: How to distinguish whether mail is incoming or outgoing?

12,924

Solution 1

Came to this page because I was having same issue in VBA. Checking the parent folders is cumbersome, as a message can be held either several folders deep (and therefore you have to iterate up several folders) or the user may have changed the folder. An extreme example: the deleted items folder contains both incoming and outgoing mail items.

I have chosen a similar solution to another person (Adi Kini) above where I check the ReceivedByName (I think he chose ReceivedEntryID). The ReceivedByName property is always Null ("") for a sent message, wherever it currently lays. This method can find a sent item that has been dragged to the inbox!. It seems a fairly reliable method of checking.

It seems odd that such an apparently straightforward thing as checking whether mail is incoming or outgoing can trip us up!

Solution 2

This is how I check mail type and it works even if mail is moved to any folder. This solution uses PROPERTY ACCESSOR which is available in outlook 2007. Below is the code

string PR_MAIL_HEADER_TAG = "http://schemas.microsoft.com/mapi/proptag/0x007D001E";

Outlook.PropertyAccessor oPropAccessor = mItemProp.PropertyAccessor;

string strHeader = (string)oPropAccessor.GetProperty(PR_MAIL_HEADER_TAG);

if (strHeader == "")
{
    // MAIL IS OF TYPE SENTBOX
}
else
{
   // MAIL IS OF TYPE INBOX
}

Solution 3

MailItem.sent is true for incoming too.

How about checking MailItem.ReceivedByEntryID. But i am sure this will fail (ReceivedByEntryID will be null for mails in inbox) if you say import from outlook express or maybe some other email program

Iterating thru mail accounts/senderemail may help as you said, but its not fool proof (like if you rename the email address)

Solution 4

I resolved this problem by adding a new UserProperty after e-mail was sent. So when I need to check if e-mail was sent I check this property. This works even if e-mail was moved out of Sent folder. Of course, this works only for newly created e-mails, but you may add this property to all e-mails in Sent folder during first start. The only bug is that UserProperties are printed by default, but this can be overridden.

Share:
12,924
Tomáš Kafka
Author by

Tomáš Kafka

Tomaskafka.com, author of Weathergraph watchface for Garmin & Pebble. linkedin.com/tomaskafka

Updated on June 18, 2022

Comments

  • Tomáš Kafka
    Tomáš Kafka almost 2 years

    I am writing VSTO Outlook addin in C#, and I need to distinguish, whether given MailItem is incoming or outgoing (or neither, when it is for example a draft).

    Is there some foolproof way to do this? Best solution I have now would be getting a list of recipients, cc's, and bcc's, loading email adresses from active accounts, and checking if those two lists intersect, but this seems quite fragile to me, and I hope that there is a better solution.

    Use case: I'd like to get a relevant date for an email, which could be either ReceivedTime, or SentOn, but to know which one I should use, I beed to know whether a mail was sent or received.

    Thank you for ideas :)

  • Tomáš Kafka
    Tomáš Kafka over 14 years
    But what if user (or mail filter rules) already moved mail to some generic folder (for example, folder for project XYZ) with both incoming and outgoing emails? In that case, incoming/outgoing would be a property specific to email, not to folder. Nice idea though, this didn't occur to me :).
  • Doug L.
    Doug L. over 14 years
    That's a very good question. I don't have a god answer for that one.
  • Tomáš Kafka
    Tomáš Kafka over 13 years
    I think that this wouldn't work on sent/received mails that have been moved to another folder (for example with a filter) - would it?
  • Tomáš Kafka
    Tomáš Kafka over 13 years
    Nope - see David's answer stackoverflow.com/questions/1285713/…
  • Tomáš Kafka
    Tomáš Kafka over 13 years
    This is a pretty heavyweight solution, but probably only one here that could work. However, the add-in would have to run all the time - otherwise we'd get an unmarked received e-mails - and on first start. we wouldn't be able to mark sent e-mails that have been moved to other folder. Maybe it would be possible to scan through all mail and see if recipient is one of user's e-mail addresses, but this could take hours on a large exchange/IMAP account...
  • Tomáš Kafka
    Tomáš Kafka almost 13 years
    Thanks! This might work for most users, as I don't think many enterprise users would have their e-mails imported from Outlook Expres (and even if they did, most of the useful operations would still only involve recent mails which would be received by Outlook).
  • cremor
    cremor over 9 years
    Note that neither ReceivedByName nor ReceivedEntryID will have a value if the mail item was received by a public folder that has an address assigned.
  • Lakedaimon
    Lakedaimon about 8 years
    works with exchange mail accounts but not with imap/smtp - tested with ol 2016
  • Dmitry Streblechenko
    Dmitry Streblechenko almost 3 years
    That won't work if you have multiple accounts in the profile - I, for example, have at several POP3/SMTP accounts delivering to an Exchange mailbox.