/// <summary>
        /// Executes a web request against the OpSkins API.
        /// </summary>
        /// <param name="url">A string containing the URL for the web request.</param>
        /// <param name="method">A string indicating the method of the web request.</param>
        /// <param name="authToken">A string containing the OpSkins authentication token to use for the request.</param>
        /// <param name="data">A string containing data relating to the web request.</param>
        /// <returns>A string containing the result returned by the web request.</returns>
        internal static string ExecuteRequest(string url, string method, string authToken, string data = null)
        {
            HttpWebRequest  Request;
            StreamReader    Reader = null;
            HttpWebResponse Response;
            string          ReturnValue = null;

            // Configure the request
            Request = (HttpWebRequest)HttpWebRequest.Create(url);
            Request.Headers.Add("authorization", "Basic " + authToken);
            Request.Method = method;

            // Add post data
            if (method == "POST" && data != null && data.Length > 0)
            {
                byte[] Data;
                Stream RequestStream;

                Data = System.Text.Encoding.ASCII.GetBytes(data);
                Request.ContentType   = "application/x-www-form-urlencoded";
                Request.ContentLength = Data.Length;

                RequestStream = Request.GetRequestStream();
                RequestStream.Write(Data, 0, Data.Length);
                RequestStream.Flush();
                RequestStream.Close();
            }

            try
            {
                Response = (HttpWebResponse)Request.GetResponse();
                try
                {
                    Reader      = new StreamReader(Response.GetResponseStream());
                    ReturnValue = Reader.ReadToEnd();
                }
                finally
                {
                    if (Reader != null)
                    {
                        Reader.Close();
                    }
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    try
                    {
                        string       WexData;
                        StreamReader WexReader;

                        WexReader   = new StreamReader(wex.Response.GetResponseStream());
                        WexData     = WexReader.ReadToEnd();
                        ReturnValue = WexData;
                        // _LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "WebException error '{0}' generated with response: {1}", wex.Message, ReturnValue);
                    }
                    catch (Exception)
                    {
                        //  _LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "WebException error '{0}' generated. Unable to process Response object with exception '{1}'", wex.Message, ex.Message);
                    }
                }
                else
                {
                    //_LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "WebException error '{0}' generated without Response object", wex.Message);
                }
            }
            catch (Exception)
            {
                //_LogWriter.LogMessage(LogFileEntryPriority.Error, "ExecuteRequest", "Exception error '{0}' generated", ex.Message);
            }

            return(ReturnValue);
        }
Beispiel #2
0
        public void ExchangeTest()
        {
            /*
             * string serverAddress = "HY1MAAEX001.HE1.LOCAL";
             * string title = "Test";
             * string emailAccount = "WindMIData";
             * string destinationFolder = @"C:\PipelineData\0.EmailFolder";
             */
            //string emailPassword = "";
            string emailServerType = "EXCHANGE";

            //SSL = "Yes";
            //port = 993;


            ExchangeClient client = null;

            if (emailServerType.ToUpper() == "EXCHANGE")
            {
                //client = new ExchangeClient("http://hy1maaex001.he1.local/owa/winmi0", "winmi0", emailPassword, "HE1");
                //client = new ExchangeClient("http://hy1maaex001.he1.local/exchange/goldt1", "goldt1", "Hannah321", "HE1");
                //client = new ExchangeClient("http://hy1maaex001.he1.local/owa/goldt1", "goldt1", "Hannah321", "HE1");
                //https://imail.bpglobal.com/exchange/europeasiarawindata/inbox/
//                client = new ExchangeClient("https://imail.bpglobal.com/exchange/europeasiarawindata/inbox", "sheam6", "p", "BP1");


                // start
                String strServerName = "imail.bpglobal.com";
                String strDomain     = "bp1";
                String strUserID     = "sheam6";
                String strPassword   = "******";
                //String strUserName = "******";
                String strUserName = "******";
                String term        = "BP";

                /*
                 * Console.Write("Server (eg imail.bpglobal.com):");
                 * strServerName = Console.ReadLine();
                 *
                 * Console.Write("Domain (eg bp1):");
                 * strDomain = Console.ReadLine();
                 *
                 * Console.Write("UserID (eg sheam6):");
                 * strUserID = Console.ReadLine();
                 *
                 * Console.Write("Password:"******"Mailbox (eg mike.sheard or europeasiarawindata):");
                 * strUserName = Console.ReadLine();
                 *
                 */
                // Create our destination URL.
                String strURL = "https://" + strServerName + "/exchange/" + strUserName + "/InBox"; // was WindData

                //strURL = "https://imail.bpglobal.com/exchange/europeasiarawindata/InBox/";


                // find unread emails

                string QUERY = "<?xml version=\"1.0\"?>"
                               + "<g:searchrequest xmlns:g=\"DAV:\">"
                               + "<g:sql>SELECT \"urn:schemas:httpmail:subject\", "
                               + "\"urn:schemas:httpmail:from\", \"DAV:displayname\", "
                               + "\"urn:schemas:httpmail:textdescription\" "
                               + "FROM SCOPE('deep traversal of \"" + strURL + "\"') "
                               + "WHERE \"DAV:ishidden\" = False AND \"DAV:isfolder\" = False "
                               + "AND \"urn:schemas:httpmail:read\" = False "
                               //+ "AND \"urn:schemas:httpmail:hasattachment\" = False "
                               //+ "\"DAV:hasattachment\"=True "
                               //////////+ "AND \"urn:schemas:httpmail:subject\" LIKE '%" + term + "%' "
                               + "ORDER BY \"urn:schemas:httpmail:date\" DESC"
                               + "</g:sql></g:searchrequest>";

                System.Net.HttpWebResponse Response = null;
                System.IO.Stream           RequestStream;
                System.IO.Stream           ResponseStream;
                System.Xml.XmlDocument     ResponseXmlDoc;
                System.Xml.XmlNodeList     SubjectNodeList;
                System.Xml.XmlNodeList     SenderNodeList;
                System.Xml.XmlNodeList     BodyNodeList;
                System.Xml.XmlNodeList     URLNodeList;

                Console.WriteLine("Mailbox URL: " + strURL);

                HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
                Request.CookieContainer = new CookieContainer();

                Request.CookieContainer.Add(AuthenticateSecureOWA(strServerName, strDomain, strUserID, strPassword));

                // Ori
                //Request.Credentials = new System.Net.NetworkCredential( strUserName, strPassword, strDomain);
                Request.Credentials = new System.Net.NetworkCredential(strUserID, strPassword, strDomain);

                //Console.WriteLine(String.Format("Got cookies for: {0}\\{1}", strDomain, strUserName));
                Console.WriteLine(String.Format("Got cookies for: {0}\\{1}", strDomain, strUserID));

                Request.Method            = "SEARCH";
                Request.ContentType       = "text/xml";
                Request.KeepAlive         = true;
                Request.AllowAutoRedirect = false;

                Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(QUERY);
                Request.Timeout       = 30000;
                Request.ContentLength = bytes.Length;
                RequestStream         = Request.GetRequestStream();
                RequestStream.Write(bytes, 0, bytes.Length);
                RequestStream.Close();
                Request.Headers.Add("Translate", "F");
                try
                {
                    Response       = (System.Net.HttpWebResponse)Request.GetResponse();
                    ResponseStream = Response.GetResponseStream();
                    //' Create the XmlDocument object from the XML response stream.
                    ResponseXmlDoc = new System.Xml.XmlDocument();
                    ResponseXmlDoc.Load(ResponseStream);
                    SubjectNodeList = ResponseXmlDoc.GetElementsByTagName("d:subject");
                    SenderNodeList  = ResponseXmlDoc.GetElementsByTagName("d:from");
                    URLNodeList     = ResponseXmlDoc.GetElementsByTagName("a:href");
                    BodyNodeList    = ResponseXmlDoc.GetElementsByTagName("d:textdescription");

                    int i = 1;
                    foreach (XmlElement subject in SubjectNodeList)
                    {
                        Console.WriteLine("WebDav result - Subject: " + subject.InnerText);
                        if (i++ >= 3)
                        {
                            break; // proved our point
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                // read attachment
                enumAttachments("https://imail.bpglobal.com/exchange/mike.sheard/Inbox/FW:%20Emailing:%20Release.zip.EML", strUserName, strPassword, strDomain);


                // end

                // try to read a list of attachments
                // Working on this bit: GetAttachmentsListXML(strServerName, "https://imail.bpglobal.com/exchange/mike.sheard/WindData/Fuxin%20Data-32.EML", "sheam6", "p", "bp1");


                Console.WriteLine("\n\nAspose Test\n\nUsing URL:" + strURL);

                //client = new ExchangeClient(strURL, strUserName, strPassword, strDomain);
                // new code goes here
                client = new ExchangeClient(strURL, strUserID, strPassword, strDomain);

                client.CookieContainer = new CookieContainer();
                client.CookieContainer.Add(AuthenticateSecureOWA(strServerName, strDomain, strUserID, strPassword));

                // Aspose client now
                //client = new ExchangeClient("https://imail.bpglobal.com/exchange/mike.sheard/inbox/", "sheam6", "p", "BP1");


                //client = new ExchangeClient("https://imail.bpglobal.com/exchange/mike.sheard/inbox/", Request.Credentials);
            }

            try
            {
                //query mailbox
                ExchangeMailboxInfo mailbox = client.GetMailboxInfo();
                //list messages in the Inbox
                ExchangeMessageInfoCollection messages = client.ListMessages(mailbox.InboxUri, false);

                int i = 1;
                foreach (ExchangeMessageInfo info in messages)
                {
                    //save the message locally
                    //client.SaveMessage(info.UniqueUri, info.Subject + ".eml");
                    Console.WriteLine("Aspose result - subject: " + info.Subject);
                    if (i++ >= 10)
                    {
                        break; // proved our point
                    }
                }
            }
            catch (ExchangeException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.ReadLine();
            }
        }