I have a basic folder in my outlook with alerts from a monitoring system. I would like to create a table based upon that data. To that end I have been trying to access the body content of emails in Outlook:
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$inbox.Items | Format-Table BodyFormat, Body, HTMLBody, RTFBody
Now I would expect to see some content for Body, HTMLBody, or RTFBody -- I do not. I DO see that BodyFormat is most often 2 (olFormatHTML). So most of the emails should have content in HTMLBody -- None of them do. I can access the subject lines but not the content.
Since every example I find online seems to be able to get the body content I think it must be a setting in outlook to only download the subject lines or something. We have Enterprise Vault -- could the content be stored separate from the message?
Based upon the MSDN documentation I believe that this is indeed the Anti-virus blocking access to selective properties via the Object Model Guard. This explains why the $mm.Body
property is settable but not gettable. Since I do not have access to any of the corporate security settings on my machine I was unable to pursue the interop direction any further.
There are a few workaround suggestions
I think I will be working with the EWS although I have to say that the interface is rather complicated.
thanks to @TessellatingHeckler for point me on the path.
With the following code you access the body of the unread mails in the file named "mailFolder".
$OL=New-Object -ComObject OUTLOOK.APPLICATION
$ns =$OL.GETNAMESPACE("MAPI")
[string]$Folder ="mailFolder"
$mail = $ns.Folders.Item(1).Folders.Item($Folder).Items.Restrict('[UnRead] = True')
$mail | Select-Object -Property Body | Format-List
well, I have multiple accounts in my outlook. I have slightly customized the code shared by Mr. MahmutKarali and it worked for me. Also, the code runs in non-admin mode of PowerShell only. After that, I have to allow access in the outlook app on my desktop for 10 mins.
$ol = New-Object -ComObject Outlook.Application
$ol.Session.Logon("Outlook")
$ns = $ol.GetNamespace("MAPI")
$mail = $ns.Folders.Item(3).Folders.Item(2).Items.Restrict('[UnRead] = True')
$mail | Select-Object -Property Body | Format-List
Here Item(3) is for getting a specific user from profiles and Item(2) is for getting the inbox folder for that user. It's all hit and trial for me to fnd these values, it can vary for you so just run the code step by step and part by part to get the desired folder of your user. I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With