I have build a script that reads out all of my public folders and saves the emails to a local storage. The script has full access rigths and does impersonate as an admin user.
At first, I did a test run where I commented out the mail.Load() row and the logic that saves the email content to the disc.
In this test run I printed out all my public folders and also all emails (Subject) and where they would be saved on my disc.
In this test run everything went fine so I did a real run. At first everything went fine but after a few hundred emails I get an error message back while trying to load the email content with mail.Load(): "the remoteserver gives back an error: 401 not autorised".
The strange thing is, that this happens in a random email in a public folder. So i can read the first ~100 emails of this folder and then this error messages pops up.
Is there some kind of protection against to many api requests that just unautorises the api so no more calls can be made for a specific time frame?
Do you guys have an idea why this 401 error is happening?
The powershell code looks somewhat like that:
$folders = @() $folders = FindAllFolders($null) foreach($projectFolder in $folders){ $view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000) $frFolderResult = $projectFolder.FindItems($sfCollection,$view) $localFolder = FindLocalFolder($projectFolder) if($localFolder -ne $null){ ##Iterate Found Items foreach ($miMailItems in $frFolderResult.Items){ $strSubject = $miMailItems.Subject $TimeStamp = $miMailItems.DateTimeSent $TmpMail = $localFolder.FullName + "\" + $TimeStamp.ToString("yyyyMMdd_HHmmss") + "-" + $strSubject + ".eml" if(-Not(Test-Path $TmpMail -PathType Leaf)) { $miMailItems.Load() #This part throws the error after a few hundred mails $mailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent) $miMailItems.Load($mailProps) #Write Item to Disc $IoFile = New-Object System.IO.FileStream($TmpMail, [System.IO.FileMode]::Create) $IoFile.Write($miMailItems.MimeContent.Content, 0, $miMailItems.MimeContent.Content.Length) $IoFile.Close() } } } }