Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I followed a suggestion from this and was able to generate successful results response. However, I am still finding it difficult to get a complete mail as .eml/msg file from that response.

Is it possible to get complete mail as a file instead of attachments ?.

What I have tried:

How to get recurrence data from JavaScript API in office365 - Stack Overflow[^]

HttpWebRequest webRequest = WebRequest.CreateHttp(request.ewsUrl);
webRequest.Headers.Add("Authorization", string.Format("Bearer {0}", request.attachmentToken));
webRequest.PreAuthenticate = true;
webRequest.AllowAutoRedirect = false;
webRequest.Method = "POST";
webRequest.ContentType = "text/xml; charset=utf-8";

// Construct the SOAP message for the GetAttchment operation.
// byte[] bodyBytes = Encoding.UTF8.GetBytes(string.Format(GetAttachmentSoapRequest, attachment.id));
byte[] itemBytes = Encoding.UTF8.GetBytes(string.Format(GetItemSoapRequest, request.ItemId, request.ChangeKey));
webRequest.ContentLength = itemBytes.Length;

Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(itemBytes, 0, itemBytes.Length);
requestStream.Close();

// Make the request to the Exchange server and get the response.
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

// If the response is okay, create an XML document from the
// response and process the request.
if (webResponse.StatusCode == HttpStatusCode.OK)
{
    Stream responseStream = webResponse.GetResponseStream();
    //using (StreamReader reader = new StreamReader(responseStream))
    //{
    //    var filePath = string.Format("F:\\data\\Mail{0}.eml", DateTime.Now.Ticks.ToString());
    //    // use whatever method you want to save the data to the file...
    //    File.AppendAllText(filePath, webResponse.Headers.ToString());
    //    File.AppendAllText(filePath, reader.ReadToEnd());
    //}
    var responseEnvelope = XElement.Load(responseStream);
    // After creating a memory stream containing the contents of the
    // attachment, this method writes the XML document to the trace output.
    // Your service would perform it's processing here.
    if (responseEnvelope != null)
    {
        var processResult = ProcessXmlResponse(responseEnvelope);
        //attachmentNames.Add(string.Format("{0} {1}", attachment.name, processResult));

    }


    //XmlDocument xmlDocument = new XmlDocument();
    //xmlDocument.Load(responseStream);
    //var path = string.Format("F:\\data\\Mail{0}.xml", DateTime.Now.Ticks.ToString());
    //xmlDocument.Save("F:\\data\\testMail1.xml");
    // This method simply writes the XML document to the
    // trace output. Your service would perform its
    // processing here.
    //Trace.Write(xmlDocument.InnerXml);

    // Close the response stream.
    responseStream.Close();
    webResponse.Close();
}



here is the soapRequest

 private const string GetItemSoapRequest =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
<soap:Header>
<t:RequestServerVersion Version=""Exchange2013"" />
</soap:Header>
  <soap:Body>
    <GetItem
      xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages""
      xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
      <ItemShape>
        <t:BaseShape>Default</t:BaseShape>
        <t:IncludeMimeContent>true</t:IncludeMimeContent>
      </ItemShape>
      <ItemIds>
        <t:ItemId Id = ""{0}"" ChangeKey=""{1}"" />
      </ItemIds>
    </GetItem>
  </soap:Body>
</soap:Envelope>";
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900