Retrieve Email For User

Welcome Forums General PowerShell Q&A Retrieve Email For User

This topic contains 4 replies, has 4 voices, and was last updated by

3 years, 2 months ago.

  • Author
  • #34330

    Points: 0
    Rank: Member

    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

    Points: 60
    Rank: Member

    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

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

    Points: 0
    Rank: Member

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

  • #34506

    Points: 0
    Rank: Member

    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..


The topic ‘Retrieve Email For User’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort