[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

How to connect Mail Exchange server and get unread mail counts?

Asked by techprocess in Exchange Email Server, SendMail Email Server, Programming for ASP.NET

Tags: ASP.NET

I want to connect (by using empployee's ID and emailid) to Exchange server and retrive count of mails that are unread by that user/employee.

Please let me know how to do it? I have impmented below code but its giving error
"The remote server returned an error: (403) Forbidden" (I have provided proper id and password)

I have also mentioned line on which I am getting error.

Regard,
VikramG
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
// Variables.
            string strInboxURI = "http://SMTP_IP/exchange/mailbox/inbox";
            string strUserName = "abc@techprocess.co.in";
            string strPassword = "abc@123";
            string strDomain = "tpsl";
            string strQuery = "";
            byte[] bytes = null;
            System.IO.Stream RequestStream = null;
            System.IO.Stream ResponseStream = null;
            XmlDocument ResponseXmlDoc = null;
            XmlNodeList DisplayNameNodes = null;
 
            try
            {
                System.Net.HttpWebRequest Request;
                System.Net.WebResponse Response;
                System.Net.CredentialCache MyCredentialCache;
 
                // Build the SQL query.
                strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >" + "<D:sql>SELECT \"DAV:displayname\" FROM \"" + strInboxURI + "\"" + "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false" + " AND \"urn:schemas:httpmail:read\" = false</D:sql></D:searchrequest>";
 
                // Create a new CredentialCache object and fill it with the network
                // credentials required to access the server.
                MyCredentialCache = new System.Net.CredentialCache();
                MyCredentialCache.Add(new System.Uri(strInboxURI), "NTLM", new System.Net.NetworkCredential(strUserName, strPassword, strDomain));
                
//----------------
                //Set the system proxy with valid server address or IP and port.
                System.Net.WebProxy pry = new System.Net.WebProxy("10.10.100.8", 8000);
                //The DefaultCredentials automically get username and password.
                pry.Credentials = new NetworkCredential("employeeid", "NTPWD", "Domain");
                
                //GlobalProxySelection.Select = pry;
//----------------
                
                // Create the HttpWebRequest object.
                Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strInboxURI);
                Request.Proxy = pry;
                // Add the network credentials to the request.
                Request.Credentials = MyCredentialCache;
 
                // Specify the method.
                Request.Method = "SEARCH";
 
                // Encode the body using UTF-8.
                bytes = Encoding.UTF8.GetBytes((string)strQuery);
 
                // Set the content header length.  This must be
                // done before writing data to the request stream.
                Request.ContentLength = bytes.Length;
 
                // Get a reference to the request stream.
                RequestStream = Request.GetRequestStream();
 
                // Write the SQL query to the request stream.
                RequestStream.Write(bytes, 0, bytes.Length);
 
                // Close the Stream object to release the connection
                // for further use.
                RequestStream.Close();
 
                // Set the content type header.
                Request.ContentType = "text/xml";
 
                // Send the SEARCH method request and get the
                // response from the server.
                Response = (HttpWebResponse)Request.GetResponse(); //HERE ITS GIVING ERROR
 
                // Get the XML response stream.
                ResponseStream = Response.GetResponseStream();
 
                // Create the XmlDocument object from the XML response stream.
                ResponseXmlDoc = new XmlDocument();
                ResponseXmlDoc.Load(ResponseStream);
 
                // Build a list of the DAV:href XML nodes, corresponding to the folders
                // in the mailbox.  The DAV: namespace is typically assgigned the a:
                // prefix in the XML response body.
                DisplayNameNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
 
                Label1.Text = "Number of Unread Messages:" + DisplayNameNodes.Count.ToString();
                // Clean up.
                ResponseStream.Close();
                Response.Close();
 
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
[+][-]10/03/09 04:35 AM, ID: 25484869Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/03/09 04:47 AM, ID: 25484898Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/03/09 04:52 AM, ID: 25484915Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/19/09 03:42 PM, ID: 25866447Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625