views:

855

answers:

5

Hi, 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 :)

A: 

Take a look at the MailItem's .Parent property - examine the folder properties to determine if it is the inbox, outbox, drafts, sent items, etc.

Doug L.
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 :).
Tomáš Kafka
That's a very good question. I don't have a god answer for that one.
Doug L.
A: 

Did you try MailItem.Sent property?

Its true for incoming, and false for outgoing.

shaibee
A: 

I came here with the same problem. Since I will explicitly suggest that user moves the mail to some folder in my usecase, checking Parent would not help...

Regarding MailItem.Sent: are you sure that MailItem.Sent works this way? I just wrote a simple code to run through both my Inbox and SentItems and for almost all of them Sent is true. I assume this is really just an indication whether the mail has been sent (= is not draft)...

davidpodhola
A: 

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)

Ady Kini
A: 

You can check if it's inside the Outlook.OlDefaultFolders.olFolderInbox or olFolderOutbox, then there should be other methods you can use to check if it's inside either of these folders!

Outlook.Application _application = new Outlook.Application();
Outlook.MAPIFolder folder = _application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Zolomon