I have been experimenting with this all day to no avail.
I can successfully retrieve a journal item from an exchange mailbox, and access the attachment which is an ItemAttachment
:
EmailMessage mailItem_Journal = EmailMessage.Bind(_service, item.Id, _propsJournalItem);
ItemAttachment itemAttachment = mailItem_Journal.Attachments[0] as ItemAttachment;
itemAttachment.Load();
Item attachedItem = itemAttachment.Item;
The Item
contained inside is of type IPM.Note i.e. an email. I need to bind to an EmailMessage
object so that I can access email specific properties, e.g. ToRecipients
which are not available otherwise.
However the Id
of attachedItem
is null, which causes the following to fail
EmailMessage mailItem = EmailMessage.Bind(_service, attachedItem.Id, _propsEmailDetail);
Value cannot be null
I tried binding to the ItemAttachment
but as expected this did not work, throwing the error
Invalid Id
I guessed maybe the Id is null because the email is embedded within another email and so isn't exactly "in" any folder where it would get "have" id, so I tried using Item.Save
to store it in the mailbox, in the hope that I could then retrieve it as normal:
attachedItem.Save(_folderItemProcessing.Id);
However this gives me the error
This operation can't be performed because this service object already has an ID. To update this service object, use the Update() method instead.
which makes no sense to me since my problem is specifically an Id which is null!
I don't understand how the Update method would be of use in this scenario, so instead I tried moving the item
attachedItem.Move(_folderItemProcessing.Id);
and this gives me
This operation isn't supported on attachments.
How can I access the email properties of the attached email? I only need to read them, I have no need to manipulate any of the objects besides moving the parent mail to a folder afterwards, which I am already doing successfully.
Side note: This code was previously implemented using the Outlook Redemption library and the mail was accessed successfully with mailItem_Journal.Attachments[1].EmbeddedMsg
, so it must be possible somehow!?
When you call ItemAttachment.Load
, that loads all of the properties into the item, there is no need to call Bind
. You should have all of the properties, but you need to cast it to an EmailMessage
. Something like:
itemAttachment.Load();
EmailMessage attachedMsg = itemAttachment.Item as EmailMessage;