...

View Full Version : webDAV Downloading Attachments from Exchange Server



Basscyst
05-03-2007, 01:15 AM
I have the following code, that retrieves the emails from a designated inbox, stores the message and subject content in a SQL database and then deletes them from the exchange server. This all works great.

Now I need to take the attachments for each email and save them on a different server.

I've researched this all day, and I keep finding references stating that I can download the file using, X-MS-ENUMATTS. I'm using it currently to retrieve a list of attached files, however I fail to see how I can download the attachment to my webserver using this.

The red code below is a request to the actual file. So I know the http path to the file, how do I go about saving it to my server?



<%
response.buffer="false"
%>
<!--#include file="includes/asp/db.inc"-->
<%
Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXMLHTTP.Open "PROPFIND", "http://192.168.0.246/exchange/ts/inbox", False,"user","password"
oXMLHTTP.SetRequestHeader "Content-Type", "xml"
oXMLHTTP.SetRequestHeader "Depth", "1"
oXMLHTTP.Send
set xmlstr=oXMLHTTP.ResponseXML
set doc=xmlstr.documentElement
set mails=doc.getElementsByTagName("a:response")
For i=0 To mails.Length-1
set Mail_Subject=mails(i).getElementsByTagName("d:subject")
If Mail_Subject.Length <> 0 Then
set Mail_body=mails(i).getElementsByTagName("e:textdescription")
set Sender=mails(i).getElementsByTagName("e:fromemail")
set o_file=mails(i).getElementsByTagName("a:href")
ExecSQL("INSERT INTO TechMails (Mail_Subject,Mail_body,Sender) VALUES('" & Replace(Mail_Subject(0).firstChild.nodeValue,"'","''") & "','" & Replace(mail_body(0).firstChild.nodeValue,"'","''") & "','" & sender(0).firstChild.nodeValue & "')")

Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXMLHTTP.Open "X-MS-ENUMATTS", o_file(0).firstChild.nodeValue, False,"user","password"
oXMLHTTP.SetRequestHeader "Content-Type", "xml"
oXMLHTTP.SetRequestHeader "Depth", "1"
oXMLHTTP.Send
set xmlstr=oXMLHTTP.ResponseXML
set doc=xmlstr.documentElement
set atts=doc.getElementsByTagName("a:response")
attlist=""
For j=0 To atts.Length-1
fpath=atts(j).getElementsByTagName("a:href")(0).firstChild.nodeValue
fname=atts(j).getElementsByTagName("e:attachmentfilename")(0).firstChild.nodeValue
ftype=atts(j).getElementsByTagName("a:prop")(0).getElementsByTagName("d:x3703001f")(0).firstChild.nodeValue

Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.4.0")
oXMLHTTP.Open "GET",fpath, False,"user","password"
oXMLHTTP.Send
xmlstr=oXMLHTTP.responseText
Next
Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.4.0")
oXMLHTTP.Open "DELETE",o_file(0).firstChild.nodeValue,False,"user","password"
oXMLHTTP.SetRequestHeader "Depth", "infinity"
oXMLHTTP.Send
set oXMLHTTP=nothing
End If
Next
%>
<techmails>
<%
set rs=getSQL("SELECT * FROM techMails")
Do Until rs.EOF
%>
<email>
<mailsender value="<%=rs.Fields("Sender")%>" />
<mailsubject><![CDATA[<%=rs.Fields("mail_subject")%>]]></mailsubject>
<mailbody><![CDATA[<%=rs.Fields("mail_body")%>]]></mailbody>
</email>
<%
rs.MoveNext
Loop
%>
</techmails>

Thanks,
Basscyst



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum