none
Move message using EWS RRS feed

  • Question

  • Hello,

    I need to move messages from inbox to another folder in the same mailbox.

    I use service account to access this mailbox. I run the script below using scheduled task under the service account 

    Somehow, the command

    [VOID]$miMailItems.Move("SentItems")

    .. moves the message to the "Sent Items" folder in the service account mailbox (if the account has mailbox enabled) instead of targeted mailbox. If the service account has no mailbox, it ends with an error.

    $MailboxName = 'targetmailbox@mydomain.com'
    
    Import-Module -Name “C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll”
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
    $Service.AutodiscoverUrl('targetmailbox@mydomain.com',{$true})
    $folderid = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
    $InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
    
    $Sfha = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true) 
    
    $sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
    $sfCollection.add($Sfha)  
    
    # Grab all the mail that meets the prerequisites
    $view = new-object Microsoft.Exchange.WebServices.Data.ItemView(100)
    $frFolderResult = $InboxFolder.FindItems($sfCollection,$view) 
    
    # Loop through the emails
    foreach ($miMailItems in $frFolderResult.Items){
    
    	# Load the message
    	$miMailItems.Load()
        
    # Loop through the attachments
    foreach($attach in $miMailItems.Attachments){  
     $attach
     $attach.Load()
     } 
     
    ### buggy code ###
        ## Mark the email as read
    	$miMailItems.isread = $true
    	$miMailItems.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
    
    	# Move the message (optional)
    	[VOID]$miMailItems.Move("SentItems")
    } 

    If I run the script under the targetedmailbox account, the message ends in correct place.

    There is no issue in loading the message, the issue is only with moving the message to another folder within same mailbox (when running the script under different credentials)

    Regards
    Mekac


    • Edited by Mekac Friday, May 24, 2019 5:49 AM
    • Moved by jrv Friday, May 24, 2019 7:00 AM Correct forum.
    • Moved by Manu Meng Monday, May 27, 2019 3:00 AM relocate
    Friday, May 24, 2019 5:48 AM

Answers

  • Instead of using 

    [VOID]$miMailItems.Move("SentItems")

     

    You should use the FolderId class with the Mailbox overload to specify the Mailbox of the Target folder you trying to move the item to (else it will be the calling account) eg

    $TargetFolderId = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$MailboxName)
    [
    VOID]$miMailItems.Move($TargetFolderId)

    Cheers
    Glen

    • Marked as answer by Mekac Tuesday, May 28, 2019 8:29 AM
    Tuesday, May 28, 2019 12:09 AM

All replies

  • Thanks for this!
    Friday, May 24, 2019 5:51 AM
  • EWS requires that the connected user has permissions on the other users mailbox/folder.


    \_(ツ)_/

    Friday, May 24, 2019 6:37 AM
  • no need to give obvious advices jrv.. Im not exchange novice...

    I already have full access to mailbox (as enterprise admin) or I added full access rights, I even have ApplicationImpersonation role.

    Btw, I wrote that I don't have problems accessing emails in the inbox folder. I can download them etc.. there is no issue in the permissions to mailbox/folders.

    Thanks for reply though.

    Friday, May 24, 2019 6:45 AM
  • "ends in an error".  What ends in what error?  Why aren't you posting this in the Exchange forum?


    \_(ツ)_/

    Friday, May 24, 2019 6:49 AM
  • The error states that the folder is not accessible (which is obvious, when the service account has no mailbox and the move command try to access it).
    Youre right, this topic should be posted in Exchange General forum.   Can u move it there as moderator, please?

    Friday, May 24, 2019 6:54 AM
  • You are now in the Exchange PowerShell forum.

    \_(ツ)_/

    Friday, May 24, 2019 7:01 AM
  • You are now in the Exchange PowerShell forum.

    \_(ツ)_/

    @jrv, Exchange Server Development forum is a more appropriate forum for this issue, I would help move the thread to this forum.

    Regards,

    Manu Meng


    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.

    Monday, May 27, 2019 3:00 AM
  • Instead of using 

    [VOID]$miMailItems.Move("SentItems")

     

    You should use the FolderId class with the Mailbox overload to specify the Mailbox of the Target folder you trying to move the item to (else it will be the calling account) eg

    $TargetFolderId = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$MailboxName)
    [
    VOID]$miMailItems.Move($TargetFolderId)

    Cheers
    Glen

    • Marked as answer by Mekac Tuesday, May 28, 2019 8:29 AM
    Tuesday, May 28, 2019 12:09 AM
  • Thank you Glen, worked like a charm.
    Tuesday, May 28, 2019 8:29 AM