none
List emails in an Office 365 mailbox RRS feed

  • Question

  • Hi,

    I could use this script to get the emails in my mailbox, But  I  need 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. (I am an Admin and already have access to all user mailboxes)

    #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)
    
    $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $sidbind = "LDAP://"
    $aceuser = [ADSI]$sidbind
    $service.AutodiscoverUrl($aceuser.mail.ToString())
    
    $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 . Thanks StrayMuse

    • Moved by jrv Thursday, July 19, 2018 2:07 PM Better forum
    • Moved by Niko.ChengModerator Friday, July 20, 2018 3:14 AM relocated
    Thursday, July 19, 2018 2:00 PM

All replies

  • Hi hdsouza,

    I'll move this thread to Exchange development forum, as they should have more professional knowledge on this and you may get effective solution timely.

    Thanks for your understanding,


    Best Regards,
    Niko Cheng


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, July 20, 2018 3:13 AM
    Moderator
  • Ok. Thanks Niko
    Friday, July 20, 2018 12:41 PM
  • You need to change the following line

    $findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$view)

    To something like

    $MailboxName = "Mailboxyouwanttoaccess@domain.com"
    $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName) 

    $inboxFolder = new-object$findResults = $service.FindItems($folderid,$view)


    Cheers
    Glen

    Monday, July 23, 2018 12:17 AM