Read emails in a shared mailbox using an admin account

Welcome Forums General PowerShell Q&A Read emails in a shared mailbox using an admin account

Viewing 1 reply thread
  • Author
    Posts
    • #213093
      Hil
      Participant
      Topics: 28
      Replies: 39
      Points: 173
      Rank: Participant

      I can use this script to read email in my own account. It works great. But if I specify an admin account (a4327) to read emails in another mailbox  $findResults still displays emails from my mailbox.

      How do i get the script to read emails from a different mailbox

       

      Here is my code:

      $Userid="a4327"
      $pass_secure = Get-Content "e:\cred_a4327.txt" | ConvertTo-SecureString
      $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $Userid, $pass_secure
      $password = $credential.GetNetworkCredential().password
      $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
      [void][Reflection.Assembly]::LoadFile($dllpath)
      $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService
      $Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($Userid,$password)
      $Sharedmail = "MessagingReports"
      $service.AutodiscoverUrl($Sharedmail,$TestUrlCallback)
      $TestUrlCallback = {
      param ([string] $url)
      if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
      }

      $service.AutodiscoverUrl($Sharedmail,$TestUrlCallback)
      $PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
      $PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
      $numOfEmailsToRead = 5
      $index = 0
      $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index)
      $findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$view)

       

       

    • #213282
      Hil
      Participant
      Topics: 28
      Replies: 39
      Points: 173
      Rank: Participant

      I managed to define the shared mailbox. That fixed it.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.