Click here to Skip to main content
11,433,220 members (60,372 online)
Click here to Skip to main content

Accessing Remote Exchange Server To read/delete mails using WEBDAV

, 5 Dec 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Accessing Remote Exchange Server To read/delete mails using WEBDAV

Overview

The article is about reading /deleting the mails, attachments from a remote exchange server, I faced this requirement when my client told me to automate the reading the undeliverable mails and deleting them after reading from his inbox without any intervention. And making an entry into the database about all these mails.

There are many ways of accessing the exchange server like outlook objects, CDO, MAPI, CODEX And Webdav. But webdav is only one which doesn’t requires any configurations as such. You have to just give the webaccess to the mail account from exchange server from which you want to read or delete mails.

References required for the project

Imports System.xml
Imports System.Xml.Xsl
Imports MSXML2  

The whole project will be dependent on the MSXML2 and its methods like

.open("PROPFIND", ConnectingURL, False, UserName, PWD)
.open("DELETE", strurl, False, UserName, PWD)
.open("GET", strattachmentUrl, False, UserName, PWD)
.open("X-MS-ENUMATTS", strUrl, False, UserName, PWD)
  • ConnectingURL:- Is the Path of your Exchange server
  • Example:- http://255.255.255.255/exchange/administrator/inbox
  • UserName:- Is the name of inbox owner
  • Pwd:- Is the password of the inbox owner.

And to implement this project you need to have sound knowledge of XML, as the methods return results as XML. So all the manipulations is done on XML only.

Declaring the variables

Please ensure that MSXML2 with version of 4.0,you can download it from MSDN.

  Dim oXmlHttp As New MSXML2.ServerXMLHTTP40
  Dim xmlDOMParams As New System.Xml.XmlDataDocument
  Dim xmlDOMParamsAttachement As New MSXML2.DOMDocument40
  Dim xmlNdLstDonation, xmlNdLstDonation1, 
    xmlNdListHasAttach As XmlNodeList
  Dim PropstatNodes As System.Xml.XmlNodeList
  Dim HrefNodes As System.Xml.XmlNodeList
  Dim StatusNode As System.Xml.XmlNode
  Dim PropNode As System.Xml.XmlNode
With oXmlHttp
            '''Open and read all the mails of inbox'''
            '.open("PROPFIND",             
            .open("PROPFIND", ConnectingURL, False, UserName, PWD)
            .setRequestHeader("Depth", "1")
            .setRequestHeader("Content-type", "xml")
            .send()
            Debug.Write(.responseBody)
            Debug.Write(.responseXML)
            str = oXmlHttp.responseText
            ''Load the read mails into XML document'''
            xmlDOMParams.LoadXml(str)
            '''Get the list of text descriptions of all the mails'''
            xmlNdLstDonation = xmlDOMParams.GetElementsByTagName(
                "e:textdescription")
            '''Get the List of Subjects of all the mails'''
            'xmlNdLstDonation1 = xmlDOMParams.GetElementsByTagName("d:subject")
            xmlNdLstDonation1 = xmlDOMParams.GetElementsByTagName("a:href")

Relevance of the output

e:textdescription:- Gives you the body of Email

a:href:- Gives you the path of each mail as emails are stored as EML files in exchange server.

For example a email in your exchange server will be stored as :--

http://255.255.255.255/exchange/administrator/inbox/emailsubject.eml

d:subject:- Gives you the subject all the emails.

e:hasattachment:- Tells you whether the email contains attachment or not.

To delete the specific email

strurl = Pass the URL of mail which you want to delete.

http://255.255.255.255/exchange/administrator/inbox/emailsubject.eml

You can extract the names of mail from a:href tag and pass it to this method to delete more than one mail.

.open("DELETE", strurl, False, UserName, PWD)
.setRequestHeader("Depth", "infinity")
.send()

Get the list of attachments of a mail

Public Function GetAttachmentsListXML(ByVal strUrl As String) As String
        Const FOLDERITEM = "Inbox"
        Dim HttpWebRequest As MSXML2.XMLHTTP40
        Dim strPropReq As String
        Dim strOutPutFile As String
        Dim xmlAttachUrl As New MSXML2.DOMDocument40
        Dim xmlNdLstDonation2 As XmlNodeList
        Dim xmlNd As XmlNode
        Dim GetAttachmentsListXML1 As String
        Dim strURL1 As String
        HttpWebRequest = New MSXML2.XMLHTTP40
        With HttpWebRequest
            .open("X-MS-ENUMATTS", strUrl, False, UserName, PWD)
            .setRequestHeader("Depth", "1")
            .setRequestHeader("Content-type", "xml")
            .send()
            GetAttachmentsListXML1 = HttpWebRequest.responseText            
        End With
        HttpWebRequest = Nothing
    End Function

You can parse this GetAttachmentsListXML1 (XML) to get the path of attachment once you get the path of attachment you can pass that path to. open("GET", strattachmentUrl, False, UserName, PWD)

ReadAnAttatchment as string
Dim HttpWebRequest As MSXML2.XMLHTTP40
HttpWebRequest = New MSXML2.XMLHTTP40
HttpWebRequest.open("GET", strattachmentUrl, False, UserName, PWD)
HttpWebRequest.send()
ReadAnAttatchment = HttpWebRequest.responseText

Hope this code will help you people. Happy coding.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

amit.arora

United States United States
No Biography provided

Comments and Discussions

 
QuestionWebDAV .NET for Exchange Pin
Tiko Monou17-Sep-12 21:32
memberTiko Monou17-Sep-12 21:32 
Questionshared mailbox Pin
Member 433230428-Feb-12 20:09
memberMember 433230428-Feb-12 20:09 
GeneralMy vote of 3 Pin
bhuvan0115-Sep-10 10:05
memberbhuvan0115-Sep-10 10:05 
GeneralRetrieving emails coming to a distribution group Pin
yahya29-Apr-10 1:12
memberyahya29-Apr-10 1:12 
GeneralRe: Retrieving emails coming to a distribution group Pin
AnubhavSharma3-Jun-11 3:36
memberAnubhavSharma3-Jun-11 3:36 
GeneralExchange Server is on SSL Pin
pipoken3-Feb-10 17:28
memberpipoken3-Feb-10 17:28 
GeneralRe: Exchange Server is on SSL Pin
amit.arora4-Feb-10 5:35
memberamit.arora4-Feb-10 5:35 
GeneralSample code Pin
Nitin Sawant30-Dec-09 22:52
memberNitin Sawant30-Dec-09 22:52 
GeneralSent Folder in exchange Pin
zigo_alcapone20-Jul-09 20:11
memberzigo_alcapone20-Jul-09 20:11 
GeneralRe: Sent Folder in exchange Pin
amit.arora4-Feb-10 5:37
memberamit.arora4-Feb-10 5:37 
GeneralMake a new folder on exchanage server Pin
usha gupta10-Apr-09 1:08
memberusha gupta10-Apr-09 1:08 
GeneralCompiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pin
jcrumble9-Apr-09 8:07
memberjcrumble9-Apr-09 8:07 
GeneralRe: Compiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pin
amit.arora9-Apr-09 9:40
memberamit.arora9-Apr-09 9:40 
GeneralRe: Compiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pin
jcrumble9-Apr-09 10:26
memberjcrumble9-Apr-09 10:26 
GeneralRe: Compiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pin
jcrumble10-Apr-09 6:29
memberjcrumble10-Apr-09 6:29 
QuestionProblems connecting 404 Pin
morphias0@yahoo.com8-Apr-09 5:00
membermorphias0@yahoo.com8-Apr-09 5:00 
AnswerRe: Problems connecting 404 Pin
morphias0@yahoo.com9-Apr-09 3:54
membermorphias0@yahoo.com9-Apr-09 3:54 
GeneralRe: Problems connecting 404 Pin
amit.arora9-Apr-09 4:34
memberamit.arora9-Apr-09 4:34 
QuestionWhat is Imports MSXML2 in C# Pin
KARYANI27-Mar-09 4:00
memberKARYANI27-Mar-09 4:00 
AnswerRe: What is Imports MSXML2 in C# Pin
amit.arora30-Mar-09 5:05
memberamit.arora30-Mar-09 5:05 
GeneralRe: What is Imports MSXML2 in C# Pin
rahulsnagle27-Jul-10 19:26
memberrahulsnagle27-Jul-10 19:26 
GeneralHTTP file transfers using WebDAV Pin
Santoshaitha18-Mar-09 18:19
memberSantoshaitha18-Mar-09 18:19 
GeneralRe: HTTP file transfers using WebDAV Pin
amit.arora19-Mar-09 4:38
memberamit.arora19-Mar-09 4:38 
QuestionWhere can we send the information about port and exchange server credentials? Pin
Sree12412-Mar-09 6:47
memberSree12412-Mar-09 6:47 
AnswerRe: Where can we send the information about port and exchange server credentials? Pin
amit.arora19-Mar-09 4:33
memberamit.arora19-Mar-09 4:33 
GeneralEmail Tracking in VB.Net Pin
usha gupta12-Jan-09 20:42
memberusha gupta12-Jan-09 20:42 
GeneralRe: Email Tracking in VB.Net Pin
amit.arora13-Jan-09 3:46
memberamit.arora13-Jan-09 3:46 
GeneralRe: Email Tracking in VB.Net Pin
usha gupta13-Jan-09 20:02
memberusha gupta13-Jan-09 20:02 
GeneralRe: Email Tracking in VB.Net Pin
amit.arora14-Jan-09 3:27
memberamit.arora14-Jan-09 3:27 
GeneralRe: Email Tracking in VB.Net Pin
usha gupta14-Jan-09 19:19
memberusha gupta14-Jan-09 19:19 
AnswerRe: Email Tracking in VB.Net Pin
amit.arora15-Jan-09 3:34
memberamit.arora15-Jan-09 3:34 
GeneralNeed to autorefresh Mail every few minutes Pin
usha gupta11-Dec-08 0:05
memberusha gupta11-Dec-08 0:05 
GeneralRe: Need to autorefresh Mail every few minutes Pin
amit.arora14-Jan-09 3:34
memberamit.arora14-Jan-09 3:34 
GeneralEdit Email Body Pin
Nuno Neto5-Dec-08 0:13
memberNuno Neto5-Dec-08 0:13 
GeneralRe: Edit Email Body Pin
Nuno Neto5-Dec-08 5:10
memberNuno Neto5-Dec-08 5:10 
GeneralRe: Edit Email Body Pin
amit.arora14-Jan-09 3:28
memberamit.arora14-Jan-09 3:28 
GeneralRe: Edit Email Body Pin
morphias0@yahoo.com9-Apr-09 5:57
membermorphias0@yahoo.com9-Apr-09 5:57 
GeneralRe: Edit Email Body Pin
amit.arora9-Apr-09 9:35
memberamit.arora9-Apr-09 9:35 
GeneralRe: Edit Email Body Pin
for120630-Nov-09 6:32
memberfor120630-Nov-09 6:32 
GeneralX-MS-ENUMATTS Pin
kobaid2-Nov-08 1:37
memberkobaid2-Nov-08 1:37 
GeneralRe: X-MS-ENUMATTS Pin
amit.arora21-Nov-08 5:41
memberamit.arora21-Nov-08 5:41 
QuestionX-MS-ENUMATTS http 403 Forbidden Pin
kobaid28-Oct-08 4:51
memberkobaid28-Oct-08 4:51 
AnswerRe: X-MS-ENUMATTS http 403 Forbidden Pin
amit.arora21-Nov-08 5:36
memberamit.arora21-Nov-08 5:36 
GeneralX-MS-ENUMATTS Pin
dafsoft1-Oct-08 3:30
memberdafsoft1-Oct-08 3:30 
GeneralRe: X-MS-ENUMATTS Pin
amit.arora21-Nov-08 5:37
memberamit.arora21-Nov-08 5:37 
General440 Login Timeout Pin
dafsoft27-Sep-08 13:11
memberdafsoft27-Sep-08 13:11 
GeneralRe: 440 Login Timeout Pin
Member 89538616-Oct-08 11:06
memberMember 89538616-Oct-08 11:06 
GeneralRe: 440 Login Timeout Pin
pollo200620-Nov-08 23:57
memberpollo200620-Nov-08 23:57 
GeneralRe: 440 Login Timeout Pin
amit.arora21-Nov-08 5:40
memberamit.arora21-Nov-08 5:40 
GeneralRe: 440 Login Timeout Pin
Member 163758727-Nov-08 1:45
memberMember 163758727-Nov-08 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 6 Dec 2004
Article Copyright 2004 by amit.arora
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid