Option Explicit On
Option Strict On
Module Module1
Sub Main()
' Variables
Dim Request As System.Net.HttpWebRequest
Dim Response As System.Net.HttpWebResponse
Dim MyCredentialCache As System.Net.CredentialCache
Dim strServer As String
Dim strPassword As String
Dim strDomain As String
Dim strUserName As String
Dim strMailboxURI As String
Dim strQuery As String
Dim bytes() As Byte
Dim RequestStream As System.IO.Stream
Dim ResponseStream As System.IO.Stream
Dim ResponseXmlDoc As System.Xml.XmlDocument
Dim HrefNodes As System.Xml.XmlNodeList
Dim SizeNodes As System.Xml.XmlNodeList
Dim iMailboxSize As Integer
Dim UserName As String = "用户名"
Dim Password As String = "密码"
Try
' Initialize variables.
strMailboxURI = "邮件服务器的地址" & "exchange/" & UserName
strUserName = UserName
strPassword = Password
strDomain = "你们的域名"
' Build the SQL query.
strQuery = " " & _
" " & _
" " & "select " _
& " ""DAV:href""" _
& ", ""DAV:displayname""" _
& ", ""urn:schemas:httpmail:unreadcount""" _
& " from scope (' shallow traversal of """ _
& strMailboxURI & """ ') " _
& " WHERE ""DAV:isfolder"" = true AND ""DAV:ishidden"" = false" & " " & " "
' 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(strMailboxURI), _
"NTLM", _
New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
)
' Create the PUT HttpWebRequest object.
Request = CType(System.Net.WebRequest.Create(strMailboxURI), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
Request.Credentials = MyCredentialCache
' Specify the SEARCH method.
Request.Method = "SEARCH"
' Encode the body using UTF-8.
bytes = System.Text.Encoding.UTF8.GetBytes(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 message body 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"
' Set the Translate header.
Request.Headers.Add("Translate", "F")
' Send the SEARCH method request and get the
' response from the server.
Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)
' Get the XML response stream.
ResponseStream = Response.GetResponseStream()
' Create the XmlDocument object from the XML response stream.
ResponseXmlDoc = New System.Xml.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.
'HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:href")
HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname")
' Build a list of the http://schemas.microsoft.com/mapi/proptag/x0e080003
' XML nodes returned in the search request. The
' http://schemas.microsoft.com/mapi/proptag/ namespace is typically
' assigned the d: prefix in the XML response body.
'SizeNodes = ResponseXmlDoc.GetElementsByTagName("d:x0e080003")
SizeNodes = ResponseXmlDoc.GetElementsByTagName("d:unreadcount")
' Loop through the list of nodes and total up the sizes of the
' mailbox folders.
Dim i As Integer
For i = 0 To HrefNodes.Count - 1
If HrefNodes(i).InnerText = "收件箱" Then
Console.WriteLine("你好,你的收件箱共有 " & SizeNodes(i).InnerText & " 封未读邮件")
End If
Next
Console.ReadLine()
' Clean up.
ResponseStream.Close()
Response.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the
' SEARCH method request on the server will be caught
' here, also.
Console.WriteLine(ex.Message)
End Try
End Sub
End Module