Click here to Skip to main content
Licence CPOL
First Posted 5 Dec 2004
Views 250,650
Bookmarked 66 times

Accessing Remote Exchange Server To read/delete mails using WEBDAV

By | 5 Dec 2004 | Article
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)

About the Author

amit.arora



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionshared mailbox PinmemberMember 433230419:09 28 Feb '12  
GeneralMy vote of 3 Pinmemberbhuvan019:05 15 Sep '10  
GeneralRetrieving emails coming to a distribution group Pinmemberyahya0:12 29 Apr '10  
GeneralRe: Retrieving emails coming to a distribution group PinmemberAnubhavSharma2:36 3 Jun '11  
GeneralExchange Server is on SSL Pinmemberpipoken16:28 3 Feb '10  
GeneralRe: Exchange Server is on SSL Pinmemberamit.arora4:35 4 Feb '10  
GeneralSample code PinmemberNitin Sawant21:52 30 Dec '09  
GeneralSent Folder in exchange Pinmemberzigo_alcapone19:11 20 Jul '09  
GeneralRe: Sent Folder in exchange Pinmemberamit.arora4:37 4 Feb '10  
GeneralMake a new folder on exchanage server Pinmemberusha gupta0:08 10 Apr '09  
GeneralCompiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pinmemberjcrumble7:07 9 Apr '09  
GeneralRe: Compiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pinmemberamit.arora8:40 9 Apr '09  
GeneralRe: Compiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pinmemberjcrumble9:26 9 Apr '09  
GeneralRe: Compiler Error Message: CS1501: No overload for method 'send' takes '0' arguments Pinmemberjcrumble5:29 10 Apr '09  
QuestionProblems connecting 404 Pinmembermorphias0@yahoo.com4:00 8 Apr '09  
AnswerRe: Problems connecting 404 Pinmembermorphias0@yahoo.com2:54 9 Apr '09  
GeneralRe: Problems connecting 404 Pinmemberamit.arora3:34 9 Apr '09  
QuestionWhat is Imports MSXML2 in C# PinmemberKARYANI3:00 27 Mar '09  
AnswerRe: What is Imports MSXML2 in C# Pinmemberamit.arora4:05 30 Mar '09  
GeneralRe: What is Imports MSXML2 in C# Pinmemberrahulsnagle18:26 27 Jul '10  
GeneralHTTP file transfers using WebDAV PinmemberSantoshaitha17:19 18 Mar '09  
GeneralRe: HTTP file transfers using WebDAV Pinmemberamit.arora3:38 19 Mar '09  
QuestionWhere can we send the information about port and exchange server credentials? PinmemberSree1245:47 12 Mar '09  
Hi Amit,
 
As per your code I am trying to implement to get emails from exchange server. I have supplied initially exchange server url like http://myexchange.com, I got the error message like "A connection with the server could not be established", Then I tried as http://myexchange.com:port, this time I got the message like "The server returned an invalid or unrecognized response".
 
Then I tried as http://myexchange.com:port/developers/inbox, here developers is one user,(I have placed even administrator also in place of developers) even then also I got the same message like "The server returned an invalid or unrecognized response".
 
Then I tried with username and password also like oXmlHttp.open("PROPFIND", "http://myexchange.com:port/developers/inbox",false,username,password);
 
Eventhough I got same message like above.
 
Please help me where I am wrong.
AnswerRe: Where can we send the information about port and exchange server credentials? Pinmemberamit.arora3:33 19 Mar '09  
GeneralEmail Tracking in VB.Net Pinmemberusha gupta19:42 12 Jan '09  

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.

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