Skip to main content

Recover Deleted Items from the Dumpster Programmatically

I started looking at this because I thought it would be a good way of finding any spam that was getting past my IMF filter that users weren't reporting. So I went of and hit the ESDK and CDO doco for any clues also did a Google crawl and didn't really turn up much except that CDO was not going to do the trick. The next step was to look at OWA and see if I could reuse any of the stuff it uses. After examining some of the conversations my browser was having with OWA I found that OWA was issuing a query with a scope of softdeleted eg "from scope ('SOFTDELETED traversal of ""') . I Googled this which turned up this Blog post from KC which started to fill in a few of the blanks.

The next thing I tried to do was use a Softdeleted traversal in a Exoledb script which although it seemed to be a valid query it didn't work or more to the point the query worked but instead of returning a record set of soft deleted items I just got all normal folder items. I tried the same thing again instead this time using MSDAIPP.dso (which uses WebDAV) and this worked so it seemed that the Softdeleted traversal only works in WebDAV and not Exoledb.

By this time I was quite interested and started thinking that I could go a bit further then just reporting on missed SPAM so I started looking at ways I could maybe copy this data out into a central repository. I started hitting a few problems because although I could access these soft deleted emails as a recordset, I couldn't open a record against any one item because as far as the mail API's where concerned the resource at that URL had been deleted. This lead me back to the OWA conversations I was looking at and I noticed when OWA restored an item from the dumpster it used the /-softdeleted-/-FlatUrlSpace-/ namespace to copy the item back into the store. I tried to access the /-softdeleted-/-FlatUrlSpace-/ directly with a ADO record but this still didn't work so I resigned to just duplicating OWA functionality which was to do a WebDav Bcopy of the items in the dumpster and then BDelete the items from the dumpster using the /-softdeleted-/-FlatUrlSpace-/ to reference the source items. The last problem was to work out what the /-softdeleted-/-FlatUrlSpace-/ property of the email was as this didn't seem to be located in the recordset either, what I found was that the /-softdeleted-/-FlatUrlSpace-/ property was the same as the /-FlatUrlSpace-/ which is available as the http://schemas.microsoft.com/exchange/permanenturl property all I needed was to append (foldername in my case deleteded items)/-softdeleted-/ before /-FlatUrlSpace-/

So what I ended up with is a script that will copy all the items from the Dumpster using a Bcopy into a folder in the users inbox and then deletes all the items from the dumpster. Its important to do the deletion because if you don't you'll end up with duplicates when the user deletes the emails again.


sDestinationURL = "http://yourserver/exchange/yourmailbox/test/"
Set XMLreq = CreateObject("Microsoft.xmlhttp")
XMLreq.open "BCOPY", "http://yourserver/exchange/yourmailbox/inbox/", False
XMLreq.setRequestHeader "Destination", sDestinationURL
xmlstr = "<?xml version=""1.0"" ?>"
xmlstr = xmlstr & "<D:copy xmlns:D=""DAV:"">"
Set Rec = CreateObject("ADODB.Record")
Set Rs = CreateObject("ADODB.Recordset")
rec.open "http://yourserver/exchange/yourmailbox", ,3
inbstr = "http://yourserver/exchange/yourmailbox/deleted items/"
strView = "select * from scope ('SOFTDELETED traversal of """ & inbstr & """') "
Rs.Open strView, rec.activeconnection, 3
If Rs.RecordCount <> 0 Then
while not rs.eof
xmlstr = xmlstr & "<D:target>"
xmlstr = xmlstr & "<D:href>" & replace(rs.fields("http://schemas.microsoft.com/exchange/permanenturl")" _
& ","/-FlatUrlSpace-/","/Deleted Items/-softdeleted-/-FlatUrlSpace-/") & "</D:href>"
xmlstr1 = xmlstr1 & "<D:href>" & replace(rs.fields("http://schemas.microsoft.com/exchange/permanenturl")" _
& ","/-FlatUrlSpace-/","/Deleted Items/-softdeleted-/-FlatUrlSpace-/") & "</D:href>"
xmlstr = xmlstr & "<D:dest>" & replace(rs.fields("Dav:href"),"Deleted%20Items","targetfolder") & "</D:dest>"
xmlstr = xmlstr & "</D:target>"
rs.movenext
wend
End If
xmlstr = xmlstr & "</D:copy>"
XMLreq.setRequestHeader "Content-Type", "text/xml;"
XMLreq.setRequestHeader "Translate", "f"
XMLreq.setRequestHeader "Content-Length:", Len(xmlstr)
XMLreq.send(xmlstr)
If (XMLreq.Status >= 200 And XMLreq.Status < 300) Then
Wscript.echo "Success! " & "Results = " & XMLreq.Status & ": " & XMLreq.statusText
ElseIf XMLreq.Status = 401 then
Wscript.echo "You don't have permission to do the job! Please check your permissions on this item."
Else
Wscript.echo "Request Failed. Results = " & XMLreq.Status & ": " & XMLreq.statusText
End If
XMLreq.open "BDELETE", "http://yourserver/exchange/yourmailbox/Deleted Items/", False
xmlstr = "<?xml version=""1.0"" ?>"
xmlstr = xmlstr & "<D:delete xmlns:D=""DAV:"">"
xmlstr = xmlstr & "<D:target>"
xmlstr = xmlstr & xmlstr1
xmlstr = xmlstr & "</D:target>"
xmlstr = xmlstr & "</D:delete>"
XMLreq.setRequestHeader "Content-Type", "text/xml;"
XMLreq.setRequestHeader "Translate", "f"
XMLreq.setRequestHeader "Content-Length:", Len(xmlstr)
XMLreq.send(xmlstr)
If (XMLreq.Status >= 200 And XMLreq.Status < 300) Then
Wscript.echo "Success! " & "Results = " & XMLreq.Status & ": " & XMLreq.statusText
ElseIf XMLreq.Status = 401 then
Wscript.echo "You don't have permission to do the job! Please check your permissions on this item."
Else
Wscript.echo "Request Failed. Results = " & XMLreq.Status & ": " & XMLreq.statusText
End If

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You need to authorize it in you tenant (eg build a small ap

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.