Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / Visual Basic
Article

Accessing Remote Exchange Server To read/delete mails using WEBDAV

Rate me:
Please Sign up or sign in to vote.
4.05/5 (17 votes)
5 Dec 2004CPOL2 min read 411.1K   66   126
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

VB.NET
Imports System.xml
Imports System.Xml.Xsl
Imports MSXML2  

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

VB.NET
.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.

VB.NET
  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.

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

Get the list of attachments of a mail

VB.NET
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)

VB.NET
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: 440 Login Timeout Pin
JohnSims19424-Jul-09 6:08
JohnSims19424-Jul-09 6:08 
QuestionHow to Look for option in the body of mail Pin
usha gupta1-Sep-08 19:44
usha gupta1-Sep-08 19:44 
AnswerRe: How to Look for option in the body of mail Pin
amit.arora21-Nov-08 4:39
amit.arora21-Nov-08 4:39 
QuestionHi Amit Pin
avani Jokhla5-Jun-08 3:23
avani Jokhla5-Jun-08 3:23 
AnswerRe: Hi Amit Pin
amit.arora21-Nov-08 4:38
amit.arora21-Nov-08 4:38 
QuestionDownload Link Pin
krsk4u4-Mar-08 1:09
krsk4u4-Mar-08 1:09 
GeneralInbox is very heavy Pin
usha gupta3-Mar-08 18:49
usha gupta3-Mar-08 18:49 
GeneralRe: Inbox is very heavy Pin
amit.arora21-Mar-08 8:18
amit.arora21-Mar-08 8:18 
QuestionNot working Pin
shiyazahammed14-Dec-07 21:03
shiyazahammed14-Dec-07 21:03 
QuestionNot working Pin
shiyazahammed14-Dec-07 21:03
shiyazahammed14-Dec-07 21:03 
QuestionNot working Pin
shiyazahammed14-Dec-07 21:01
shiyazahammed14-Dec-07 21:01 
QuestionNot working http://1server/exchange/user2/inbox/emailsubject.eml Pin
shiyazahammed14-Dec-07 21:01
shiyazahammed14-Dec-07 21:01 
GeneralRe: Not working http://1server/exchange/user2/inbox/emailsubject.eml Pin
amit.arora21-Mar-08 8:22
amit.arora21-Mar-08 8:22 
GeneralMSXML2 Pin
EdwardQ8-Aug-07 2:43
EdwardQ8-Aug-07 2:43 
AnswerRe: MSXML2 Pin
amit.arora8-Aug-07 6:00
amit.arora8-Aug-07 6:00 
QuestionHow do i store attachment in its respective format like gif, doc...... [modified] Pin
wiproraja9-Jul-07 0:52
wiproraja9-Jul-07 0:52 
AnswerRe: How do i store attachment in its respective format like gif, doc...... Pin
amit.arora8-Aug-07 6:03
amit.arora8-Aug-07 6:03 
Generalsend Email Pin
Member 282129220-Feb-07 1:34
Member 282129220-Feb-07 1:34 
GeneralWhy using InterOp MSXML instead of native HttpWebRequest Pin
dhdiez4-Dec-06 1:11
dhdiez4-Dec-06 1:11 
GeneralRe: Why using InterOp MSXML instead of native HttpWebRequest Pin
amit.arora4-Dec-06 4:34
amit.arora4-Dec-06 4:34 
GeneralGet all messages of the all users of MS Exchange server with WebDav Pin
jacofl22-Nov-06 23:05
jacofl22-Nov-06 23:05 
GeneralERROR: A connection to the server cannot be established Pin
momo_lang13-Nov-06 16:48
momo_lang13-Nov-06 16:48 
GeneralRe: ERROR: A connection to the server cannot be established Pin
jacofl22-Nov-06 22:28
jacofl22-Nov-06 22:28 
QuestionHow to mark mail as read? Pin
rurouniRonin9-Oct-06 22:50
rurouniRonin9-Oct-06 22:50 
AnswerRe: How to mark mail as read? Pin
DannSmith20-Oct-06 7:37
DannSmith20-Oct-06 7:37 

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

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