Manda un correo II


bueno este codigo de la seccion anterior fue solo para leer la informacion de los correos, aparte tenemos q leer correo por correo y attachment por attachment y bajar los archivos al disco usando esta funcion:

 

private static void ReadAttach(string uri, string path, string sender, NetworkCredential credential)

        {

            System.Net.HttpWebRequest Request;

            System.Net.WebResponse Response;

            string strRootURI = uri;

            System.IO.Stream ResponseStream = null;

            XmlDocument ResponseXmlDoc = null;

            XmlNodeList DisplayNameNodes = null;

 

            System.Net.WebClient downloader = new WebClient();

            System.Net.CredentialCache MyCredentialCache;

            MyCredentialCache = new System.Net.CredentialCache();

            MyCredentialCache.Add(new System.Uri(strRootURI), "NTLM", credential);

            Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);

            Request.Credentials = MyCredentialCache;

            Request.Method = "X-MS-ENUMATTS";

            Request.ContentType = "xml";

            Request.Timeout = 600000;

            Response = (HttpWebResponse)Request.GetResponse();

            ResponseStream = Response.GetResponseStream();

            ResponseXmlDoc = new XmlDocument();

            ResponseXmlDoc.Load(ResponseStream);

            DisplayNameNodes = ResponseXmlDoc.GetElementsByTagName("a:href");

 

            for (int i = 0; i < DisplayNameNodes.Count; i++)

            {

                downloader.Credentials = MyCredentialCache;

                downloader.DownloadFile(new Uri(DisplayNameNodes[i].InnerText), path + sender.Replace("@", "(at)") + ‘;’ + HttpUtility.UrlDecode(DisplayNameNodes[i].InnerText).Split(‘/’)[DisplayNameNodes[i].InnerText.Split(‘/’).Length – 1]);

            }

            ResponseStream.Close();

            Response.Close();

        }

 

 

 

Ya para terminar, solo tienes q marcar los correos q usaste como leidos, (no hay boton derecho):

 

private static void SetRead(string uri,NetworkCredential credential)

        {

            System.Net.HttpWebRequest Request;

            System.Net.WebResponse Response;

            string strSrcURI = uri;

            string strBody = "";

            byte[] bytes = null;

            System.IO.Stream RequestStream = null;

            System.IO.Stream ResponseStream = null;

            XmlDocument ResponseXmlDoc = null;

            XmlNodeList DisplayNameNodes = null;

 

            strBody = " <?xml version=\"1.0\" encoding=\"utf-8\" ?>"

                       + " <D:propertyupdate xmlns:b=’urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/’ xmlns:e=’urn:schemas:httpmail:’ xmlns:z=’urn:schemas:mailheader:’ xmlns:c=’xml:’ xmlns:f=’http://schemas.microsoft.com/exchange/&#8217; xmlns:g=’urn:schemas-microsoft-com:office:office’ xmlns:h=’http://schemas.microsoft.com/repl/&#8217; xmlns:i=’urn:schemas-microsoft-com:exch-data:’ xmlns:D=’DAV:’>"

                       + " <D:set><D:prop><e:read b:dt=’boolean’>1</e:read></D:prop></D:set>"

                       + " </D:propertyupdate>";

            System.Net.CredentialCache MyCredentialCache;

            MyCredentialCache = new System.Net.CredentialCache();

            MyCredentialCache.Add(new System.Uri(strSrcURI), "NTLM", credential);

            Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strSrcURI);

            Request.Credentials = MyCredentialCache;

            Request.Method = "PROPPATCH";

            bytes = Encoding.UTF8.GetBytes((string)strBody);

            Request.ContentLength = bytes.Length;

            RequestStream = Request.GetRequestStream();

            RequestStream.Write(bytes, 0, bytes.Length);

            RequestStream.Close();

            Request.ContentType = "text/xml";

            Request.Timeout = 600000;

            Response = (HttpWebResponse)Request.GetResponse();

            ResponseStream = Response.GetResponseStream();

            ResponseXmlDoc = new XmlDocument();

            ResponseXmlDoc.Load(ResponseStream);

            DisplayNameNodes = ResponseXmlDoc.GetElementsByTagName("a:status");

            if (DisplayNameNodes.Count > 0)

            {

                Console.WriteLine(DisplayNameNodes[0].InnerText);

            }

            else

            {

                Console.WriteLine("DAV:displayname property not found…");

            }

            ResponseStream.Close();

            Response.Close();

        }

 

Realmente no es tan complicado, pero cuando no tenemos la informacion suficiente nos puede tomar mucho tiempo averiguar como acomodar el codigo… espero q alguien le sirvan estos ejemplos no como yo no encontre muchos q me sirvieran…

Advertisements