none
How to get specific email from Exchange 2007 by script? RRS feed

  • Question

  • Hello all!

    I have a following situation: I have one user with mailbox in Exchange 2007. There is some script that each several minutes sends one mail with some subject name to this use. I need to create a script that will look for this subject name in inbox of this user and after finds it will delete it.

    How can i create script, that reads emails of this user from Exchange 2007 and not from Outlook?

    Thanks!

    Sunday, April 15, 2012 6:03 AM

All replies

  • You can use the EWS Managed API and Powershell http://www.microsoft.com/download/en/details.aspx?id=13480. To do what you want try something like

    ## Load Managed API dll  
    Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"  
      
    ## Set Exchange Version  
    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1  
      
    ## Create Exchange Service Object  
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)  
      
    ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials  
      
    #Credentials Option 1 using UPN for the windows Account  
    $creds = New-Object System.Net.NetworkCredential("user@domain.com","password")   
    $service.Credentials = $creds      
      
    #Credentials Option 2  
    #service.UseDefaultCredentials = $true  
      
    ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates  
      
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}  
      
    ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use  
    
    
    $MailboxName = "user@domain.com"
    $SubjectToSearch = "test"
      
    #CAS URL Option 1 Autodiscover  
    $service.AutodiscoverUrl($MailboxName,{$true})  
    "Using CAS Server : " + $Service.url   
       
    #CAS URL Option 2 Hardcoded  
      
    #$uri=[system.URI] "https://casservername/ews/exchange.asmx"  
    #$service.Url = $uri    
      
    ## Optional section for Exchange Impersonation  
      
    #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, "email@domain.com")
    
    #Bind to Inbox    
    $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)    
       
    #Define the properties to get    
    $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
    #Define AQS Search String  
      
    $SearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject,$SubjectToSearch);  
      
    #Define ItemView to retrive just 1000 Items    
    $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)    
    $fiItems = $null    
    do{    
        $fiItems = $service.FindItems($Inbox.Id,$SearchFilter,$ivItemView)    
        [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)  
        foreach($Item in $fiItems.Items){      
            "Subject     : " + $Item.Subject  
    		"DateRecieved: " + $Item.DateTimeReceived
        }    
        $ivItemView.Offset += $fiItems.Items.Count    
    }while($fiItems.MoreAvailable -eq $true)    

    If you want to delete the Item with the Foreach look include something like

     $Item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)

    Cheers
    Glen

    Monday, April 16, 2012 6:16 AM