Retrieve Email For User

Tagged: ,

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Brian B Brian B 1 year, 5 months ago.

  • Author
  • #34330
    Profile photo of Tee

    What I would like to do is retrieve the email for a specific user(generic account). I'm not sure if that's possible. Currently, I'm able to get and read email from my own inbox and parse out the information that I want but I want to be able to impersonate another user. I plan on running this as a task. Below is what I currently have.
    Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
    $olFolder = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
    $outlook = New-Object -ComObject outlook.application
    $namespace = $outlook.GetNameSpace("MAPI")
    $folder = $namespace.getDefaultFolder($olFolder::olFolderInBox)
    $folder = $namespace
    $items = $folder.Items | Where-Object {$_.UnRead -eq $true}

    foreach($f in $items)
    { $reader = $null
    $reader = New-Object System.IO.StringReader($f.Body)

    $line = $reader.ReadLine()
    if($line -ne $null)
    [int]$startingIndex = $line.IndexOf(':') + 1
    [int]$stringLength = $line.IndexOf('(') – $startingIndex
    $user = $line.Substring($startingIndex,$stringLength).Trim()
    Write-Verbose -Message "User: $user"

    #Send me an email on this user
    Send-SpecialEmailAboutUser -$useract $user
    #set the message to be marked as read.
    $f.UnRead = $false


    Write-Host "Done..."

  • #34331
    Profile photo of Tim Pringle
    Tim Pringle

    Hi Tee,

    To be honest, if it's just for one specific user, your best option is just for the user to give access their mailbox to you, or them to setup a rule which will carry out the specific requirements you have.

    You could try and run script via an Invoke-Command impersonating the user though, but never tried this myself. I'm not certain whether it bypasses or uses the mail profile settings in place on the system.

  • #34332
    Profile photo of Don Jones
    Don Jones

    No, Invoke-Command doesn't load a full user profile. The Outlook COM object, which is what you're using, isn't so hot at cross-mailbox access, either. I'd probably opt for the Exchange server-side cmdlets if I _had_ to do this programmatically, but honestly, it's a read edge case in terms of automation support.

  • #34378
    Profile photo of Tee

    Thanks guys. I didn't think cross-mailbox access was possible.

  • #34506
    Profile photo of Brian B
    Brian B

    I usually use the EWS Api for Exchange, assuming you're using an Exchange server for your e-mail.

    You can use powershell to interact with the APIs and connect to a mailbox and do pretty much whatever you want. Move items, retrieve an attachment from an e-mail with a specific subject, etc..


You must be logged in to reply to this topic.