Mandando un correo I


Q fasil es enviar un correo hoy en dia, usando hotmail u Outlook, pero q pasaria si no tenemos ni uno de los dos(web o cliente), o si te dicen q mandes un correo pero q no puedes instalar ningun cliente de correo ademas deberia ser automatizado….. bueno tendriamos q programarnos el nuestro nosotros mismos… una pequeña funcion para enviar correos usando el protocolo WebDav:
 
 

static void sendEmail()

        {

            try

            {

                string strServer = "255.255.255.255";

                string strSenderAlias = "tu alias";

                string strFrom = "tu alias@tu dominio.com";

                string strTo = "destinatario alias@dest dominio.com";

 

                string strSubject = "Send Using HttpWebRequest";

                string strBody = "Hello World";

 

                string sUri;

                sUri = "http://" + strServer + "/Exchange/" + strSenderAlias;

                sUri = sUri + "/%23%23DavMailSubmissionURI%23%23/";

 

                System.Uri myUri = new System.Uri(sUri);

                HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

 

                string sQuery;

                sQuery = "From: " + strFrom + "\n" +

                 "To: " + strTo + "\n" +

                 "Subject: " + strSubject + "\n" +

                 "Date: " + DateTime.Now.ToString() + "\n" +

                 "X-Mailer: My DAV mailer" + "\n" +

                 "MIME-Version: 1.0" + "\n" +

                 "Content-Type: text/plain;" + "\n" +

                 "Charset = \"iso-8859-1\"" + "\n" +

                 "Content-Transfer-Encoding: 7bit" + "\n" + "\n" +

                 strBody;

                NetworkCredential myCred = new NetworkCredential(@"dominio\user", "password");

                CredentialCache myCredentialCache = new CredentialCache();

                myCredentialCache.Add(myUri, "Basic", myCred);

                HttpWRequest.Credentials = myCredentialCache;

 

                HttpWRequest.Headers.Set("Translate", "f");

                HttpWRequest.ContentType = "message/rfc822";

                HttpWRequest.ContentLength = sQuery.Length;

 

                HttpWRequest.Timeout = 300000;

                HttpWRequest.Method = "PUT";

 

                byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);

                HttpWRequest.ContentLength = ByteQuery.Length;

                Stream QueryStream = HttpWRequest.GetRequestStream();

                QueryStream.Write(ByteQuery, 0, ByteQuery.Length);

                QueryStream.Close();

 

                HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();

                int iStatCode = (int)HttpWResponse.StatusCode;

                string sStatus = iStatCode.ToString();

                string sReqHeaders = HttpWRequest.Headers.ToString();

 

                Stream strm = HttpWResponse.GetResponseStream();

                StreamReader sr = new StreamReader(strm);

                string sText = sr.ReadToEnd();

                Console.WriteLine("Response: {0}", sText);

 

                strm.Close();

                myCred = null;

                myCredentialCache = null;

                HttpWRequest = null;

                HttpWResponse = null;

                QueryStream = null;

                strm = null;

                sr = null;

            }

            catch (Exception e)

            {

                Console.WriteLine("{0} Exception caught.", e);

            }

        }

 

 

 

Bueno por si esto no fuera poco ahora tienes q guardar todos los attachments q recibes en una carpeta, esta es la funcion:

 

 

public static void BrowseInbox(string path, string server, string user,string userid,string password)

        {

            // Variables.

            System.Net.HttpWebRequest Request;

            System.Net.WebResponse Response;

            string strRootURI = "http://" + server + "/Exchange/" + user + "/inbox/";

            string strUserName = userid;

            string strPassword = password;

            string strDomain = "corp";

            string strQuery = "";

            byte[] bytes = null;

            System.IO.Stream RequestStream = null;

            System.IO.Stream ResponseStream = null;

            XmlDocument ResponseXmlDoc = null;

            XmlNodeList DisplayNameNodes = null;

 

            strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"

            + "<D:sql>select "

            + "\"urn:schemas:httpmail:fromemail\""

            + ", \"urn:schemas:httpmail:read\""

            + " from scope (‘shallow traversal of "

            + (char)(34) + strRootURI + (char)(34) + "’) WHERE \"urn:schemas:httpmail:read\" = false AND \"urn:schemas:httpmail:hasattachment\" = true "

            + "  </D:sql></D:searchrequest>";

            System.Net.CredentialCache MyCredentialCache;

            MyCredentialCache = new System.Net.CredentialCache();

            MyCredentialCache.Add(new System.Uri(strRootURI), "NTLM", new NetworkCredential(strUserName,strPassword,strDomain));

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

            Request.Credentials = MyCredentialCache;

            Request.Method = "SEARCH";

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

            Request.ContentLength = bytes.Length;

            Request.Timeout = 600000;

            RequestStream = Request.GetRequestStream();

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

            RequestStream.Close();

            Request.ContentType = "text/xml";

            Response = (HttpWebResponse)Request.GetResponse();

            ResponseStream = Response.GetResponseStream();

            ResponseXmlDoc = new XmlDocument();

            ResponseXmlDoc.Load(ResponseStream);

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

            if (DisplayNameNodes.Count > 0)

            {

                Console.WriteLine("Non-folder item display names…");

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

                {

                    SetRead(DisplayNameNodes[i]["a:href"].InnerXml, new NetworkCredential(strUserName, strPassword,strDomain));

                    ReadAttach(DisplayNameNodes[i]["a:href"].InnerXml, path, DisplayNameNodes[i]["a:propstat"].GetElementsByTagName("a:prop")[0]["d:fromemail"].InnerText, new NetworkCredential(strUserName, strPassword, strDomain));

                }

            }

            else

            {

                Console.WriteLine("No non-folder items found…");

            }

            ResponseStream.Close();

            Response.Close();

        }

 

 

 

Advertisements