Partager via


Listing Unread Messages in a Folder Using ADO

Topic Last Modified: 2009-07-27

Example

VBScript

Note

The following example uses a file URL with the Exchange OLE DB (ExOLEDB) provider. The ExOLEDB provider also supports The HTTP: URL Scheme. Using The HTTP: URL Scheme allows both client and server applications to use a single URL scheme.

Note

The following example provides a user name and password when opening the recordset. However, the ExOLEDB provider always operates within the security context of the running application, and does not support authentication to another user name and password. If the application security context has insufficient permission to access the mailbox, using this sample as shown may return errors such as: Run-time error '-2147217843 (80040e0d)': Authentication failed. You can work around this by using WebDAV or Microsoft OLE DB Provider for Internet Publishing (MSDAIPP), which has the ability to authenticate by user name and password.

' Listing Unread Messages in a Folder Using ADO
' This ASP page loops through all of the messages in the inbox and outputs the unread messages
' in an HTML table.

<%@ Language=VBScript %>
<%
Dim Rec
Dim Rs
Dim strURL
Dim strQ
Dim strSubj
Dim DomainName
Dim strLocalPath
Dim UnreadCount

Set Rec = CreateObject("ADODB.Record")
Set Rs = CreateObject("ADODB.Recordset")

' Time can be changed for folders with more messages.
Session.Timeout = 10

' Set your own values to the following variables:
DomainName = "nrnobledom2"

UserID ="User1"
PSW ="pass"

strLocalPath = "MBX/" & UserID & "/inbox"

strURL = "file://./backofficestorage/" & DomainName & "/" & strLocalPath
Response.Write "strURL= " & strURL


Rec.Open strURL,,,,,UserID,PSW

' Build the SQL query for the messages.
strQ = "select "
strQ = strQ & " ""urn:schemas:mailheader:date"""
strQ = strQ & ", ""urn:schemas:httpmail:to"""
strQ = strQ & ", ""urn:schemas:httpmail:from"""
strQ = strQ & ", ""urn:schemas:mailheader:subject"""
strQ = strQ & ", ""urn:schemas:mailheader:received"""
strQ = strQ & ", ""urn:schemas:httpmail:read"""
strQ = strQ & ", ""DAV:contentclass"""
strQ = strQ & ", ""DAV:href"""
strQ = strQ & " from scope ('shallow traversal of "
strQ = strQ & Chr(34) & strURL & Chr(34) & "') "
strQ = strQ & " WHERE ""urn:schemas:httpmail:read"" = false"

' Open the recordset.
Rs.Open strQ, Rec.ActiveConnection

Rs.MoveFirst
Unreadcount = 0

' Loop through all of the messages in the recordset and format them in a table.
Do Until Rs.EOF
Response.Write "<TABLE BORDER=2 cellpadding=2 cellspacing=2 width='100%'><TR>"
Response.Write "<tr><td><b>From: </b></td>"
Response.Write "<td>" & Rs.Fields("urn:schemas:httpmail:from").Value & "</td></tr>"
Response.Write "<tr><td><b>To: </b></td>"
Response.Write "<td>" & Rs.Fields("urn:schemas:httpmail:to").Value & "</td></tr>"
Response.Write "<tr><td><b>Subject: </b></td>"
Response.Write "<td>" & Rs.Fields("urn:schemas:mailheader:subject").Value & "</td></tr>"
Response.Write "<tr><td><b>Date Sent: </b></td>"
Response.Write "<td>" & Rs.Fields("urn:schemas:mailheader:date").Value & "</td></tr>"
Response.Write "<tr><td><b>Date Received: </b></td>"
Response.Write "<td>" & Rs.Fields("urn:schemas:mailheader:date").Value & "</td></tr>"
Response.Write "<tr><td><b>Content-Class: </b></td>"
Response.Write "<td>" & Rs.Fields("DAV:contentclass").Value & "</td></tr>"
Response.Write "<tr><td><b>URL: </b></td>"
Response.Write "<td>" & Rs.Fields("DAV:href").Value & "</td></tr>"
Response.Write "</p>"
unreadcount = unreadcount + 1
Rs.MoveNext
Response.Write "<tr><td height=10></tr></td><tr><td height=10></tr></td>"
Response.Write "</TABLE><br>"
Loop

' Close the record and recordset.
Rs.Close
Rec.Close

Response.Write "<hr size=5><b>Total unread messages: </b>" & unreadcount


%>