3

I'm working on a small application in which we need to transfer mailbox emails, contacts, etc from one mailbox to another. Our emails have attachments also. As per Microsoft EWS documentation the best method to export/import from exchange server to exchange server is using EWS ExportItem and UploadItems operations.

So just for testing purposes, I'm exporting a single email (which has a .zip attachment) using its ItemId. The server responded with success but I did not get the full data response that I can parse into XML and read the data element (so that I can import it into a different mailbox on an exchange using a UploadItem operation).

I'm sorry I'm just a newbie and don't know much about EWS or Managed API. Here's my sample code here:

const string getServiceConfigurationRequest =
          "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
          "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
          "               xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\"\n" +
          "               xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" \n" +
          "               xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
          "               xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
          "  <soap:Header>\n" +
          "    <t:RequestServerVersion Version=\"Exchange2013\" />\n" +
          "  </soap:Header>\n" +
          "  <soap:Body>\n" +
          "    <m:ExportItems>\n" +
          "      <m:ItemIds>\n" +
          "        <t:ItemId Id=\"AAMkAGI2NzU1NzFhLThjYTgtNDk5OS04YTc0LTU3OGZiZmM=\"/>\n" +
          "      </m:ItemIds>\n" +
          "    </m:ExportItems>\n" +

          "  </soap:Body>\n" +
          "</soap:Envelope>";

       
byte[] payload = System.Text.Encoding.UTF8.GetBytes(getServiceConfigurationRequest);

try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://outlook.office365.com/ews/exchange.asmx");
    request.AllowAutoRedirect = false;
    request.Credentials = new NetworkCredential("example@adress.net", "example");
    request.Method = "POST";
    request.ContentType = "text/xml";
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(payload, 0, payload.Length);
    requestStream.Close();

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        string responseFromServer = reader.ReadToEnd();
        Console.WriteLine("You will need to parse this response to get the configuration information:\n\n" + responseFromServer);
        reader.Close();
        responseStream.Close();
    }
    else
        throw new WebException(response.StatusDescription);
}
catch (WebException e)
{
    Console.WriteLine(e.Message);
}

I appreciate it if you guide me in the right direction. Thanks

Update: Here is the response I get from the exchange server but when I parse the response string as XMLDocument it gives me an invalid XML result:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="20" MajorBuildNumber="5164" MinorBuildNumber="20" Version="V2018_01_08" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</s:Header>
<s:Body>
  <m:ExportItemsResponse 
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">    <m:ResponseMessages>
<m:ExportItemsResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
    <m:ItemId Id="AAMkAAAAAEMAAAZMQQM1kD9Qo92S6+/w3TyAAAGfkrPAAA=" ChangeKey="CQAAAA=="/>
    <m:Data>
        VERY BIG ENCODED DATA STRING HERE
    <m:Data>
</m:ExportItemsResponseMessage>
</m:ResponseMessages>
</m:ExportItemsResponse>
</s:Body>
</s:Envelope>
  • It might help if you would post the response XML – Dmitry Streblechenko Apr 15 '22 at 15:26
  • That looks like a perfectly valid XML response. The `Data` node contains base 64 encoded blob. Are you expecting XML there? – Dmitry Streblechenko Apr 15 '22 at 21:18
  • @DmitryStreblechenko I'm trying to import this exported item to another mailbox using the **UploadItems** operation. so could you please tell me how to do this? As per my understanding, I've to create another request but this time with the UploadItem operation but I don't know how to create a proper XML for this. I'm simply following the XML pattern for import operation as shown in the Microsoft Documentation. [link](https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-import-items-by-using-ews-in-exchange) – Muhammad Hassan Javed Apr 15 '22 at 22:15
  • You just need to upload the same data you got from ExportItems. Do not attempt to parse or modify it in any way. – Dmitry Streblechenko Apr 15 '22 at 22:25
  • @DmitryStreblechenko I've tried what you just said but it gives me an exception (The operation has timed out) on `request.GetResponse()`. So, to resolve this error I added this: `request.Timeout = Timeout.Infinite; request.KeepAlive = true;` but now it gives me **Internal Server Error** on the same method. Do you know how to resolve this? – Muhammad Hassan Javed Apr 16 '22 at 17:45
  • Hard to say without actually debugging. Check if it works in OutlookSpy (I am its author - https://www.dimastr.com/outspy): click GetItem in the EWS group, go to the ExportItems tab, click "Save Data" to save the blob. Then click GetFolder button, go to the UploadItems tab, click "File to upload", then "Execute UploadItems". – Dmitry Streblechenko Apr 16 '22 at 23:07

0 Answers0