List emails in an Office 365 mailbox

Welcome Forums General PowerShell Q&A List emails in an Office 365 mailbox

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

Hil
 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #104642
    Hil

    Participant
    Points: 1
    Rank: Member

    Hi, I could use this script to get the emails in my mailbox, But do I get the messages in any mailbox. How can i specify the alias or smtp address of the mailbox to get the emails within the mailbox

    #Load the API DLL
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
    [void][Reflection.Assembly]::LoadFile($dllpath)
    
    #Create a new service object
    $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
    
    # Fancy Way to Check the user and dynamically assign the Autodiscover URL
    $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $sidbind = "LDAP://"
    $aceuser = [ADSI]$sidbind
    $service.AutodiscoverUrl($aceuser.mail.ToString())
    
    #Do some stuff
    
    $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
    $viewCount = 60
    $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($viewCount)
    $findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$view)
    
    
    $i = 0
    #$cpsFound = 0
    while ($i -le ($viewCount - 1))
    {
    $currdate = Get-Date
        #$findResults.Items[$i].Id |fl *
        if($findResults.Items[$i].DateTimeSent.ToShortDateString() -eq $currdate.ToShortDateString())
        {
            if($findResults.Items[$i].Subject.Contains("STRING_UR_LOOKING_4") -eq $True)
            {
             
            "Subject : " + $findResults.Items[$i].Subject
    
            $EmailItemID = $findResults.Items[$i].Id
            $currEmailItem = [Microsoft.Exchange.WebServices.Data.Item]::Bind($service,$EmailItemID)   
            $currSubject = $findResults.Items[$i].Subject
            $myParseCmd = $currSubject.Split("|")
           
            #$currEmailItem.Sender.Address
    
            } 
            }    
            
         # Increment the while loop 
            $i +=1
        } 
    

    This script was taken from https://social.technet.microsoft.com/Forums/windowsserver/en-US/2b116f1b-30b7-4d14-8d5e-e4832920ad2d/export-email-header-information-via-powershell

  • #104653

    Participant
    Points: 206
    Helping Hand
    Rank: Participant

    This is not a PS issue. It is a permissions issue in O365/AAD.

    Unless you are a global admin in O365/AAD or Exchange Admin or have been granted the Exchange Application Impersonation role rights, you cannot look into another users mailbox. Looking into, acting on another user mailbox requires delegated right directly or via Exchange AppImpersonation.

    Imagine the security implications / ramifications if any random user / dev to just pop open anyone mailbox. Not a good thing.

    MS provides many docs on Exchange AppImpersonation.

    'technet.microsoft.com/en-us/library/dd776119(v=exchg.150).aspx'
    'docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/impersonation-and-ews-in-exchange'
    'blogs.msdn.microsoft.com/webdav_101/2012/06/27/the-importance-of-ews-impersonation-while-using-an-application-account'

  • #104678
    Hil

    Participant
    Points: 1
    Rank: Member

    I am already an admin and have full rights to all user mailboxes.

    Any ideas on the correct syntax?

  • #104855
    Hil

    Participant
    Points: 1
    Rank: Member

    I was able to get autodiscover to work, but $inbox returns the Inbox of the admin account.
    How can i get it to return the Inbox of the mobileprint shared mailbox.
    Any ideas?

    [Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll")
    $s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
    $AdminName = "a1234@abcdef.com"
    $Pass = Get-Content "cred_.txt" | ConvertTo-SecureString
    $s.Credentials = New-Object Net.NetworkCredential($AdminName , $Pass)
    $s.AutodiscoverUrl("mobileprint@abcdef.com", {$true})
    $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
    $inbox
    

The topic ‘List emails in an Office 365 mailbox’ is closed to new replies.