WebDAV download attachments

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have two questions:
1) If there are more than one attachments, can I still use
urn:schema:httpmail:attachmentfilename to get the file names? How?
Or I need to use X-MS-ENUMATTS method?

2) After I get the filenames of the attachments, how do I download them to a
local folder?

Thanks in advance.

Li
 
Hi Li,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Li,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to download
attachments using WebDAV. If there is any misunderstanding, please feel
free to let me know.

We can use X-MS-ENUMATTS to go through the attachment list. Use a GET to
extract the stream, which can be save to file.

Here I found a sample:

' VB Example on enumerating and reading attatchments

' TODO: Add a reference to MS XML 4.0.

' TODO: Add a command button (cmdGetAttachmentsList) to a form.

' TODO: Add a multiline text box (txtResponse) with scroll bars to the
form.

' TODO: Change code as per TODO under the command button.

' TODO: Do the TODO sections at the bottom of this document which involve
creating a class.

'

' Also please review the following article, which shows some differences
to note for .NET:

' Managing Microsoft Exchange 2000 Calendars with XML Web Services

'
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnmes2k/htm
l/calwp_0001.asp

'

Private Sub cmdGetAttachmentsList_Click()

Dim sUserName As String ' Domain/Username for server

Dim sPassword As String ' User's password

Dim sBaseURL As String

Dim sMessage As String

Dim sAttatch As String

Dim sXML As String

Dim aList As Collection

Dim anInst As CAttatchmentList

Dim sOutput As String

txtResponse.Text = ""



' TODO: Change the next four lines to match your user, password and
email

sUserName = "myuser" ' User

sPassword = "mypassword" ' Password

sBaseURL =
"http://myserver.mycompany.com/exchange/Administrator/Inbox/" ' Location of
email.

sMessage = "Test%20Attatch.EML" ' HREF of email I'm going to read



sXML = GetAttachmentsListXML(sBaseURL & sMessage, sUserName, sPassword)

sOutput = "-------------------------------------------------" & vbCrLf
& _

"The XML response:" & vbCrLf & vbCrLf & sXML & vbCrLf &
vbCrLf



Set aList = ParseAttatchmentListXML(sXML)



For Each anInst In aList

sOutput = sOutput &
"-------------------------------------------------" & vbCrLf

sOutput = sOutput & "******* Attatchment: " & anInst.href & vbCrLf

sOutput = sOutput & " attachmentfilename: " &
anInst.attachmentfilename & vbCrLf

sOutput = sOutput & " cn: " & anInst.cn & vbCrLf

sOutput = sOutput & " Attatchment Text: " & vbCrLf

sOutput = sOutput & ReadAnAttatchment(anInst.href, sUserName,
sPassword)

sOutput = sOutput & vbCrLf

Next



Set aList = Nothing



txtResponse.Text = sOutput

End Sub



'---------------------------------------------------------------------------

' GetAttachmentsListXML

' Searches an HREF to an email to find attatchments for the email

' Input

' sHREF - Http reference to the email

' sUserName - Domain/Username for server

' sPassword - Password for the account above

' Returns: An XML document resulting from the search

'---------------------------------------------------------------------------

Function GetAttachmentsListXML(ByVal sHREF As String, ByVal sUserName As
String, ByVal sPassword As String) As String

Const FOLDERITEM = "Inbox/Test1.eml"

Dim HttpWebRequest As Msxml2.XMLHTTP30

Dim strPropReq As String

Dim strOutPutFile As String



Set HttpWebRequest = New Msxml2.XMLHTTP30



With HttpWebRequest

.Open "X-MS-ENUMATTS", sHREF, False, sUserName, sPassword

.setRequestHeader "Content-type:", "text/xml"

.setRequestHeader "Depth", "1,noroot"

.Send



GetAttachmentsListXML = HttpWebRequest.ResponseText

End With



Set HttpWebRequest = Nothing

End Function



'---------------------------------------------------------------------------

' ReadAnAttatchment

' Reads the contents of an attatchment for an email.

' Input

' sHREF - Http reference to the email

' sUserName - Domain/Username for server

' sPassword - Password for the account above

' Returns: The contents of the email attatchment

'---------------------------------------------------------------------------

Private Function ReadAnAttatchment(ByVal sHREF As String, ByVal sUserName
As String, ByVal sPassword As String) As Variant



Dim HttpWebRequest As Msxml2.XMLHTTP30

Dim vReturn As Variant



Set HttpWebRequest = New Msxml2.XMLHTTP30

HttpWebRequest.Open "GET", sHREF, False, sUserName, sPassword



HttpWebRequest.Send



ReadAnAttatchment = HttpWebRequest.ResponseText ' Returns as text

'ReadAnAttatchment = HttpWebRequest.responseBody ' Returns as encoded



Set HttpWebRequest = Nothing

End Function



'---------------------------------------------------------------------------

' ParseAttatchmentListXML

' This will parse the XML document containing the list Attatchment names

' Input:

' sXML - An XML document string containing a list of email attatchments

' Returns:

' A collection of Email attatchemnt references (collection of
CAttatchmentList classes)

'---------------------------------------------------------------------------

Private Function ParseAttatchmentListXML(ByVal sXML As String) As Collection

Dim doc As Msxml2.DOMDocument

Set doc = New Msxml2.DOMDocument



Dim aNode As IXMLDOMNode

Dim aSecondNode As IXMLDOMNode

Dim aThirdNode As IXMLDOMNode

Dim aFourthNode As IXMLDOMNode

Dim aFifthNode As IXMLDOMNode

Dim sHREF As String

Dim sAttachmentFileName As String

Dim sCN As String

Dim lListCount As Long

lListCount = 1



sHREF = ""

sAttachmentFileName = ""

sCN = ""



Dim xmlRoot As IXMLDOMElement

Dim xmlNode As IXMLDOMNode

Dim xmlReq As Msxml2.XMLHTTP40

Dim objNodeList As IXMLDOMNodeList

Dim anInstance As New CAttatchmentList





Dim MyClasses As New Collection ' Create a Collection object.



doc.loadXML (sXML)

Set xmlRoot = doc.documentElement '.documentElemen



Set objNodeList = xmlRoot.selectNodes("//a:multistatus/a:response")
' Select what we are after



For Each aNode In objNodeList

If aNode.hasChildNodes Then

For Each aSecondNode In aNode.childNodes ' 2

Debug.Print "Second: " & aSecondNode.nodeName;

'Debug.Print "Third: " & aThirdNode.nodeName;

If aSecondNode.nodeName = "a:href" Then

sHREF = aSecondNode.Text ' RESET.

sAttachmentFileName = "" ' RESET.

sCN = "" ' RESET

End If

If aSecondNode.hasChildNodes Then

For Each aThirdNode In aSecondNode.childNodes ' 3



If aThirdNode.hasChildNodes Then

For Each aFourthNode In aThirdNode.childNodes '
4

If aFourthNode.hasChildNodes Then

For Each aFifthNode In
aFourthNode.childNodes

Debug.Print aFourthNode.nodeName

Select Case aFourthNode.nodeName

Case "e:attachmentfilename"

sAttachmentFileName =
aFifthNode.Text

Case "i:cn"

sCN = aFifthNode.Text

End Select

Next ' 5

End If

Next ' 4

End If

Next ' 3

End If



If sHREF <> "" And sAttachmentFileName <> "" And sCN <> ""
Then

Set anInstance = New CAttatchmentList

anInstance.href = sHREF

anInstance.attachmentfilename = sAttachmentFileName

anInstance.cn = sCN

MyClasses.Add anInstance, CStr(lListCount)

lListCount = lListCount + 1

sHREF = aSecondNode.Text ' RESET.

sAttachmentFileName = "" ' RESET.

sCN = "" ' RESET

End If

Next '2

End If

Next ' 1



Set ParseAttatchmentListXML = MyClasses

End Function





' TODO: Create a class file called CMailList and paste the code below in.

'Public Class CMailList



Public href As String

Public attachmentfilename As String

Public cn As String





If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi, Kevin:

Thanks a lot!
I will try it.

Li

Kevin Yu said:
Hi Li,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to download
attachments using WebDAV. If there is any misunderstanding, please feel
free to let me know.

We can use X-MS-ENUMATTS to go through the attachment list. Use a GET to
extract the stream, which can be save to file.

Here I found a sample:

' VB Example on enumerating and reading attatchments

' TODO: Add a reference to MS XML 4.0.

' TODO: Add a command button (cmdGetAttachmentsList) to a form.

' TODO: Add a multiline text box (txtResponse) with scroll bars to the
form.

' TODO: Change code as per TODO under the command button.

' TODO: Do the TODO sections at the bottom of this document which involve
creating a class.

'

' Also please review the following article, which shows some differences
to note for .NET:

' Managing Microsoft Exchange 2000 Calendars with XML Web Services

'
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnmes2k/htm
l/calwp_0001.asp

'

Private Sub cmdGetAttachmentsList_Click()

Dim sUserName As String ' Domain/Username for server

Dim sPassword As String ' User's password

Dim sBaseURL As String

Dim sMessage As String

Dim sAttatch As String

Dim sXML As String

Dim aList As Collection

Dim anInst As CAttatchmentList

Dim sOutput As String

txtResponse.Text = ""



' TODO: Change the next four lines to match your user, password and
email

sUserName = "myuser" ' User

sPassword = "mypassword" ' Password

sBaseURL =
"http://myserver.mycompany.com/exchange/Administrator/Inbox/" ' Location of
email.

sMessage = "Test%20Attatch.EML" ' HREF of email I'm going to read



sXML = GetAttachmentsListXML(sBaseURL & sMessage, sUserName, sPassword)

sOutput = "-------------------------------------------------" & vbCrLf
& _

"The XML response:" & vbCrLf & vbCrLf & sXML & vbCrLf &
vbCrLf



Set aList = ParseAttatchmentListXML(sXML)



For Each anInst In aList

sOutput = sOutput &
"-------------------------------------------------" & vbCrLf

sOutput = sOutput & "******* Attatchment: " & anInst.href & vbCrLf

sOutput = sOutput & " attachmentfilename: " &
anInst.attachmentfilename & vbCrLf

sOutput = sOutput & " cn: " & anInst.cn & vbCrLf

sOutput = sOutput & " Attatchment Text: " & vbCrLf

sOutput = sOutput & ReadAnAttatchment(anInst.href, sUserName,
sPassword)

sOutput = sOutput & vbCrLf

Next



Set aList = Nothing



txtResponse.Text = sOutput

End Sub



'---------------------------------------------------------------------------

' GetAttachmentsListXML

' Searches an HREF to an email to find attatchments for the email

' Input

' sHREF - Http reference to the email

' sUserName - Domain/Username for server

' sPassword - Password for the account above

' Returns: An XML document resulting from the search

'---------------------------------------------------------------------------

Function GetAttachmentsListXML(ByVal sHREF As String, ByVal sUserName As
String, ByVal sPassword As String) As String

Const FOLDERITEM = "Inbox/Test1.eml"

Dim HttpWebRequest As Msxml2.XMLHTTP30

Dim strPropReq As String

Dim strOutPutFile As String



Set HttpWebRequest = New Msxml2.XMLHTTP30



With HttpWebRequest

.Open "X-MS-ENUMATTS", sHREF, False, sUserName, sPassword

.setRequestHeader "Content-type:", "text/xml"

.setRequestHeader "Depth", "1,noroot"

.Send



GetAttachmentsListXML = HttpWebRequest.ResponseText

End With



Set HttpWebRequest = Nothing

End Function



'---------------------------------------------------------------------------

' ReadAnAttatchment

' Reads the contents of an attatchment for an email.

' Input

' sHREF - Http reference to the email

' sUserName - Domain/Username for server

' sPassword - Password for the account above

' Returns: The contents of the email attatchment

'---------------------------------------------------------------------------

Private Function ReadAnAttatchment(ByVal sHREF As String, ByVal sUserName
As String, ByVal sPassword As String) As Variant



Dim HttpWebRequest As Msxml2.XMLHTTP30

Dim vReturn As Variant



Set HttpWebRequest = New Msxml2.XMLHTTP30

HttpWebRequest.Open "GET", sHREF, False, sUserName, sPassword



HttpWebRequest.Send



ReadAnAttatchment = HttpWebRequest.ResponseText ' Returns as text

'ReadAnAttatchment = HttpWebRequest.responseBody ' Returns as encoded



Set HttpWebRequest = Nothing

End Function



'---------------------------------------------------------------------------

' ParseAttatchmentListXML

' This will parse the XML document containing the list Attatchment names

' Input:

' sXML - An XML document string containing a list of email attatchments

' Returns:

' A collection of Email attatchemnt references (collection of
CAttatchmentList classes)

'---------------------------------------------------------------------------

Private Function ParseAttatchmentListXML(ByVal sXML As String) As Collection

Dim doc As Msxml2.DOMDocument

Set doc = New Msxml2.DOMDocument



Dim aNode As IXMLDOMNode

Dim aSecondNode As IXMLDOMNode

Dim aThirdNode As IXMLDOMNode

Dim aFourthNode As IXMLDOMNode

Dim aFifthNode As IXMLDOMNode

Dim sHREF As String

Dim sAttachmentFileName As String

Dim sCN As String

Dim lListCount As Long

lListCount = 1



sHREF = ""

sAttachmentFileName = ""

sCN = ""



Dim xmlRoot As IXMLDOMElement

Dim xmlNode As IXMLDOMNode

Dim xmlReq As Msxml2.XMLHTTP40

Dim objNodeList As IXMLDOMNodeList

Dim anInstance As New CAttatchmentList





Dim MyClasses As New Collection ' Create a Collection object.



doc.loadXML (sXML)

Set xmlRoot = doc.documentElement '.documentElemen



Set objNodeList = xmlRoot.selectNodes("//a:multistatus/a:response")
' Select what we are after



For Each aNode In objNodeList

If aNode.hasChildNodes Then

For Each aSecondNode In aNode.childNodes ' 2

Debug.Print "Second: " & aSecondNode.nodeName;

'Debug.Print "Third: " & aThirdNode.nodeName;

If aSecondNode.nodeName = "a:href" Then

sHREF = aSecondNode.Text ' RESET.

sAttachmentFileName = "" ' RESET.

sCN = "" ' RESET

End If

If aSecondNode.hasChildNodes Then

For Each aThirdNode In aSecondNode.childNodes ' 3



If aThirdNode.hasChildNodes Then

For Each aFourthNode In aThirdNode.childNodes '
4

If aFourthNode.hasChildNodes Then

For Each aFifthNode In
aFourthNode.childNodes

Debug.Print aFourthNode.nodeName

Select Case aFourthNode.nodeName

Case "e:attachmentfilename"

sAttachmentFileName =
aFifthNode.Text

Case "i:cn"

sCN = aFifthNode.Text

End Select

Next ' 5

End If

Next ' 4

End If

Next ' 3

End If



If sHREF <> "" And sAttachmentFileName <> "" And sCN <> ""
Then

Set anInstance = New CAttatchmentList

anInstance.href = sHREF

anInstance.attachmentfilename = sAttachmentFileName

anInstance.cn = sCN

MyClasses.Add anInstance, CStr(lListCount)

lListCount = lListCount + 1

sHREF = aSecondNode.Text ' RESET.

sAttachmentFileName = "" ' RESET.

sCN = "" ' RESET

End If

Next '2

End If

Next ' 1



Set ParseAttatchmentListXML = MyClasses

End Function





' TODO: Create a class file called CMailList and paste the code below in.

'Public Class CMailList



Public href As String

Public attachmentfilename As String

Public cn As String





If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
You're welcome, Li. If you have any questions, please feel free to post
them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top