|
|
|
date: Tue, 28 Aug 2007 01:06:13 -0700,
group: microsoft.public.exchange2000.development
back
Retrieve attachment from Exchange Server 2003
Dear All,
With the code below I am able to successfully pull out e-mails by using the sql query defined with the webdav's search method. However, I am very unclear how to pull the attachments from an e-mail. I have gone through various tutorials and based on which I have written the code below using the "X-MS-ENUMATTS" method. Also printed the output below. I would appreciate if you help me from the point where I can pull the attachment from the email and save it locally.
OUTPUT:-
http://extra/Exchange/user/Inbox/email.EML/1_multipart_xF8FF_
2_attachment.docHTTP/1.1 200 OKattachment.doc1-1application/msword8attachment205440 attachment.docattachment.doc
CODE:-
Option Explicit On
Option Strict On
Imports System.Reflection
Imports System.Xml
Module Module1
Sub Main()
' Variables
Dim Request As System.Net.HttpWebRequest
Dim Response As System.Net.HttpWebResponse
Dim MyCredentialCache As System.Net.CredentialCache
Dim strPassword As String
Dim strDomain As String
Dim strUserName As String
Dim strRootURI As String
Dim strQuery As String
Dim bytes() As Byte
Dim RequestStream As System.IO.Stream
Dim ResponseStream As System.IO.Stream
Dim XmlReader As System.Xml.XmlTextReader
Dim y As String
Try
' Initialize variables.
strUserName = "user"
strPassword = "pass"
strDomain = "dubaimerc.int"
strRootURI = "http://extra/Exchange/user/Inbox"
' Build the SQL query.
strQuery = "<?xml version=""1.0""?>" & _
"<D:searchrequest xmlns:D = ""DAV:"" >" & _
"<D:sql>SELECT ""dav:href"",""dav:urn:schemas:httpmail:fromemail"" FROM scope('shallow traversal of """ & _
strRootURI & """ ')"
strQuery = strQuery & " WHERE ""urn:schemas:httpmail:fromemail"" = 'cp@cp.com' and ""urn:schemas:httpmail:read""=true ORDER BY ""urn:schemas:httpmail:datereceived"" DESC"
strQuery = strQuery & "</D:sql></D:searchrequest>"
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strRootURI), _
"NTLM", _
New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
)
' Create the SEARCH HttpWebRequest object.
Request = CType(System.Net.WebRequest.Create(strRootURI), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
Request.Credentials = MyCredentialCache
' Specify the SEARCH method.
Request.Method = "SEARCH"
' Encode the body using UTF-8.
bytes = System.Text.Encoding.UTF8.GetBytes(strQuery)
' Set the content header length. This must be
' done before writing data to the request stream.
Request.ContentLength = bytes.Length
' Get a reference to the request stream.
RequestStream = Request.GetRequestStream()
' Write the message body to the request stream.
RequestStream.Write(bytes, 0, bytes.Length)
' Close the Stream object to release the connection
' for further use.
RequestStream.Close()
' Set the Content Type header.
Request.ContentType = "text/xml"
' Send the SEARCH method request and get the
' response from the server.
Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)
' Get the XML response stream.
ResponseStream = Response.GetResponseStream()
' Create the XmlTextReader object from the XML
' response stream.
XmlReader = New System.Xml.XmlTextReader(ResponseStream)
' Read through the XML response, node by node.
While (XmlReader.Read())
XmlReader.GetAttribute("a:href")
' Look for the opening DAV:href node. The DAV: namespace is
' typically assigned the a: prefix in the XML response body.
If XmlReader.Name = "a:href" Then
' Advance the reader to the text node.
XmlReader.Read()
' Display the value of the DAV:href text node.
Dim emailName As String
emailName = XmlReader.Value
Dim HttpWebRequest As MSXML2.XMLHTTP40
Dim GetAttachmentsListXML1 As String
HttpWebRequest = New MSXML2.XMLHTTP40
With HttpWebRequest
.open("X-MS-ENUMATTS", emailName, False, strUserName, strPassword)
.setRequestHeader("Depth", "1")
.setRequestHeader("Content-type", "xml")
.send()
GetAttachmentsListXML1 = HttpWebRequest.responseText
Dim x As New Xml.XmlDocument
Dim strAttachmentPath As String
x.LoadXml(GetAttachmentsListXML1)
strAttachmentPath = x.DocumentElement.InnerText
'y = x.DocumentElement.InnerText
Dim z As Object
z = ReadAnAttatchment(strAttachmentPath, strUserName, strPassword)
Console.WriteLine(strAttachmentPath)
End With
Console.WriteLine("")
HttpWebRequest = Nothing
' Advance the reader to the closing DAV:href node.
XmlReader.Read()
End If
End While
Console.ReadLine()
' Clean up.
XmlReader.Close()
ResponseStream.Close()
Response.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the
' SEARCH method requests on the server will be caught
' here, also.
Console.WriteLine(ex.Message)
Console.Read()
End Try
End Sub
Public Function ReadAnAttatchment(ByVal sHREF As String, ByVal sUserName As String, ByVal sPassword As String) As Object
Dim HttpWebRequest As MSXML2.XMLHTTP30
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
Return ReadAnAttatchment
HttpWebRequest = Nothing
End Function
End Module
EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
date: Tue, 28 Aug 2007 01:06:13 -0700
author: Mariam A.R.
Re: Retrieve attachment from Exchange Server 2003
What's the actual issue your having with the code you posted, the process of
extracting an attachment is you would issue a X-MS-ENUMATTS request that
would then return a list of the attachment that a particular message has.
You then do a get on the URL of the attachment you want to download. This
get will return a stream of bytes representing the attachment which you then
need to write to a file (using a streamwriter etc) which is what your
ReadAnAttatchment function look like it will return.
So when you run the code do you get any issue, errors , status etc ?
Cheers
Glen
<Mariam A.R.> wrote in message news:20078284611mariam_ar@eim.ae...
> Dear All,
>
> With the code below I am able to successfully pull out e-mails by using
> the sql query defined with the webdav's search method. However, I am very
> unclear how to pull the attachments from an e-mail. I have gone through
> various tutorials and based on which I have written the code below using
> the "X-MS-ENUMATTS" method. Also printed the output below. I would
> appreciate if you help me from the point where I can pull the attachment
> from the email and save it locally.
>
> OUTPUT:-
>
> http://extra/Exchange/user/Inbox/email.EML/1_multipart_xF8FF_
> 2_attachment.docHTTP/1.1 200
> OKattachment.doc1-1application/msword8attachment205440
> attachment.docattachment.doc
>
>
> CODE:-
>
> Option Explicit On
> Option Strict On
> Imports System.Reflection
> Imports System.Xml
>
>
> Module Module1
>
> Sub Main()
>
> ' Variables
> Dim Request As System.Net.HttpWebRequest
> Dim Response As System.Net.HttpWebResponse
> Dim MyCredentialCache As System.Net.CredentialCache
> Dim strPassword As String
> Dim strDomain As String
> Dim strUserName As String
> Dim strRootURI As String
> Dim strQuery As String
> Dim bytes() As Byte
> Dim RequestStream As System.IO.Stream
> Dim ResponseStream As System.IO.Stream
> Dim XmlReader As System.Xml.XmlTextReader
> Dim y As String
>
> Try
> ' Initialize variables.
> strUserName = "user"
> strPassword = "pass"
> strDomain = "dubaimerc.int"
> strRootURI = "http://extra/Exchange/user/Inbox"
>
> ' Build the SQL query.
> strQuery = "<?xml version=""1.0""?>" & _
> "<D:searchrequest xmlns:D = ""DAV:"" >" & _
> "<D:sql>SELECT
> ""dav:href"",""dav:urn:schemas:httpmail:fromemail"" FROM scope('shallow
> traversal of """ & _
> strRootURI & """ ')"
>
>
> strQuery = strQuery & " WHERE
> ""urn:schemas:httpmail:fromemail"" = 'cp@cp.com' and
> ""urn:schemas:httpmail:read""=true ORDER BY
> ""urn:schemas:httpmail:datereceived"" DESC"
> strQuery = strQuery & "</D:sql></D:searchrequest>"
>
>
>
>
> ' Create a new CredentialCache object and fill it with the
> network
> ' credentials required to access the server.
> MyCredentialCache = New System.Net.CredentialCache
> MyCredentialCache.Add(New System.Uri(strRootURI), _
> "NTLM", _
> New System.Net.NetworkCredential(strUserName, strPassword,
> strDomain) _
> )
>
> ' Create the SEARCH HttpWebRequest object.
> Request = CType(System.Net.WebRequest.Create(strRootURI), _
> System.Net.HttpWebRequest)
>
> ' Add the network credentials to the request.
> Request.Credentials = MyCredentialCache
>
> ' Specify the SEARCH method.
> Request.Method = "SEARCH"
>
> ' Encode the body using UTF-8.
> bytes = System.Text.Encoding.UTF8.GetBytes(strQuery)
>
> ' Set the content header length. This must be
> ' done before writing data to the request stream.
> Request.ContentLength = bytes.Length
>
> ' Get a reference to the request stream.
> RequestStream = Request.GetRequestStream()
>
> ' Write the message body to the request stream.
> RequestStream.Write(bytes, 0, bytes.Length)
>
> ' Close the Stream object to release the connection
> ' for further use.
> RequestStream.Close()
>
> ' Set the Content Type header.
> Request.ContentType = "text/xml"
>
> ' Send the SEARCH method request and get the
> ' response from the server.
> Response = CType(Request.GetResponse(),
> System.Net.HttpWebResponse)
>
> ' Get the XML response stream.
> ResponseStream = Response.GetResponseStream()
>
> ' Create the XmlTextReader object from the XML
> ' response stream.
> XmlReader = New System.Xml.XmlTextReader(ResponseStream)
>
> ' Read through the XML response, node by node.
>
> While (XmlReader.Read())
>
> XmlReader.GetAttribute("a:href")
>
>
> ' Look for the opening DAV:href node. The DAV: namespace
> is
> ' typically assigned the a: prefix in the XML response
> body.
>
> If XmlReader.Name = "a:href" Then
>
> ' Advance the reader to the text node.
> XmlReader.Read()
>
> ' Display the value of the DAV:href text node.
> Dim emailName As String
> emailName = XmlReader.Value
>
> Dim HttpWebRequest As MSXML2.XMLHTTP40
>
>
>
> Dim GetAttachmentsListXML1 As String
>
> HttpWebRequest = New MSXML2.XMLHTTP40
> With HttpWebRequest
> .open("X-MS-ENUMATTS", emailName, False,
> strUserName, strPassword)
> .setRequestHeader("Depth", "1")
> .setRequestHeader("Content-type", "xml")
> .send()
>
> GetAttachmentsListXML1 =
> HttpWebRequest.responseText
> Dim x As New Xml.XmlDocument
> Dim strAttachmentPath As String
> x.LoadXml(GetAttachmentsListXML1)
> strAttachmentPath = x.DocumentElement.InnerText
>
> 'y = x.DocumentElement.InnerText
>
> Dim z As Object
> z = ReadAnAttatchment(strAttachmentPath,
> strUserName, strPassword)
>
>
>
> Console.WriteLine(strAttachmentPath)
> End With
>
> Console.WriteLine("")
> HttpWebRequest = Nothing
> ' Advance the reader to the closing DAV:href node.
> XmlReader.Read()
> End If
> End While
>
>
> Console.ReadLine()
> ' Clean up.
> XmlReader.Close()
> ResponseStream.Close()
> Response.Close()
>
>
>
>
> Catch ex As Exception
>
> ' Catch any exceptions. Any error codes from the
> ' SEARCH method requests on the server will be caught
> ' here, also.
> Console.WriteLine(ex.Message)
> Console.Read()
> End Try
>
> End Sub
> Public Function ReadAnAttatchment(ByVal sHREF As String, ByVal
> sUserName As String, ByVal sPassword As String) As Object
>
> Dim HttpWebRequest As MSXML2.XMLHTTP30
>
> 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
> Return ReadAnAttatchment
>
> HttpWebRequest = Nothing
> End Function
>
> End Module
>
>
>
>
> EggHeadCafe - .NET Developer Portal of Choice
> http://www.eggheadcafe.com
date: Wed, 29 Aug 2007 17:14:58 +1000
author: Glen Scales [MVP]
|
|