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>