Updates from May, 2006 Toggle Comment Threads | Keyboard Shortcuts

  • montelof 6:49 pm on May 29, 2006 Permalink | Reply  

    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…

     
  • montelof 6:49 pm on May 29, 2006 Permalink | Reply  

    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://&quot; + 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://&quot; + 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();

            }

     

     

     

     
  • montelof 12:11 pm on May 5, 2006 Permalink | Reply  

    Un Arbolito 

    Este es el codigo de un sistema q trabaje una semana, se usa para modificar los archivos de un servidor desde una pagina web, no pude pegar todo el codigo pero auqui esta la funcion principal:
     
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

        {

            //TODO:Copia de los valores del archivo actual;

            FileData tempData = new FileData();

            checksum.ClassCheckSumFile chksumFile = new checksum.ClassCheckSumFile(this.ChecksumFilePath);

            bool Exists = false;

           

            tempData.week = this.txtWeek.Text;       

            tempData.year = this.txtYear.Text;

            //if the year is for in the full value:2006

            if (tempData.year.Length > 2)

                tempData.year = tempData.year.Substring(2,2);

            else if (tempData.year.Length < 2)

                tempData.year = "0"+ tempData.year;

     

            if (this.CheckBox1.Checked)

                this.txtFileName.Text = tempData.week + "_" + tempData.year + "_" + this.txtFileName.Text;

     

            tempData.filename = this.txtFileName.Text;

            tempData.supplier = this.DropDownListSupplier.SelectedValue;

     

            TreeNode selectedNode = TreeView1.SelectedNode;

            if (selectedNode != null)

            {

                TreeNode topnode = selectedNode;

                //searching the root Node

                while (topnode.Parent != null) topnode = topnode.Parent;

                //construction the destination path form the text box typed by the user

                string newPath = this.Path + tempData.supplier + "\\" + tempData.week + " " + tempData.year + "\\";

                string OldPath = "";

                //constructing the source path

                if (ODMFile.week!=null)

                    OldPath = this.Path + ODMFile.supplier + "\\" + ODMFile.week + " " + ODMFile.year + "\\";

                else

                    OldPath = this.Path + ODMFile.supplier + "\\Unassigned\\";

                //if the destination path dont exists,create it.

                if (!Directory.Exists(newPath))

                    Directory.CreateDirectory(newPath);

                //if there is no error, continue

                if (Directory.Exists(newPath))

                {  //moving the file

     

                    Exists = File.Exists(newPath + tempData.filename);

     

                    if ((OldPath != newPath) && (ODMFile.filename == tempData.filename) && (Exists))

                    {

                        chksumFile.AddRecord(OldPath, new FileInfo(OldPath + ODMFile.filename), "navigator", checksum.ClassCheckSumFile.Action.REMOVED);

                        chksumFile.AddRecord(newPath, new FileInfo(newPath + tempData.filename), "navigator", checksum.ClassCheckSumFile.Action.REPLACED);

                    }

                   

                    if ((OldPath != newPath) && (ODMFile.filename == tempData.filename) && (!Exists))

                        chksumFile.AddRecord(OldPath, new FileInfo(OldPath + ODMFile.filename), navigatorma, checksum.ClassCheckSumFile.Action.REMOVED);

                    if (OldPath + ODMFile.filename != newPath + tempData.filename)

                    {

                        if (Exists)

                            File.Move(newPath + tempData.filename, this.DeletedPath + DateTime.Now.Ticks.ToString() + "-" + tempData.filename);

     

                        File.Move(OldPath + ODMFile.filename, newPath + tempData.filename);

                    }

                    else

                    {

                        return;

                    }

                    if ((OldPath != newPath) && (ODMFile.filename == tempData.filename) && (!Exists))

                        chksumFile.AddRecord(newPath, new FileInfo(newPath + ODMFile.filename), "navigator", checksum.ClassCheckSumFile.Action.MOVED);

     

                    if (ODMFile.filename != tempData.filename)

                        chksumFile.AddRecord(newPath, new FileInfo(newPath + tempData.filename), "navigator", checksum.ClassCheckSumFile.Action.MOVED);

     

                    //remaning file

                    //refresing the tree,

                    TreeNode UpdatedNode = this.TreeView1.FindNode(Server.HtmlEncode(selectedNode.ValuePath));

                    TreeNode childNode=new TreeNode();

                    UpdatedNode.Parent.ChildNodes.Remove(UpdatedNode);

                    //search the week

                    UpdatedNode = this.TreeView1.FindNode(Server.HtmlEncode("folder:" + tempData.supplier + "\\" + "folder:" + tempData.week + " " + tempData.year ));                //this.TreeView1.Nodes.Clear();

                    //if the week not exist, add it

                    if (UpdatedNode == null)

                    {

                        UpdatedNode = this.TreeView1.FindNode(Server.HtmlEncode("folder:" + tempData.supplier));                //this.TreeView1.Nodes.Clear();

                        childNode= new TreeNode(tempData.week + " " + tempData.year,"folder:" + tempData.week + " " + tempData.year);

                        UpdatedNode.ChildNodes.Add(childNode);

                        UpdatedNode = childNode;

                    }

                    //node to be added

                    childNode = new TreeNode(tempData.filename, "file:" + tempData.filename);               

                    //if the node already exists, remove it

                    if (Exists)

                        UpdatedNode.ChildNodes.Remove(TreeView1.FindNode(UpdatedNode.ValuePath + "\\" + childNode.Value));

     

                    //then add again as normal flow

                    UpdatedNode.ChildNodes.Add(childNode);

                    childNode.Select();

                    childNode.ImageUrl = "~/metric15_2.gif";

                    //always check the parent for null value

                    if (childNode.Parent != null)

                        childNode.Parent.Expand();

                    else

                        childNode.Expand();

                }

            }

        }

     }

     

     

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel