Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Exchange
2000.active.directory
2000.admin
2000.announcements
2000.app.conversion
2000.applications
2000.clients
2000.clustering
2000.connectivity
2000.development
2000.documentation
2000.general
2000.information.store
2000.interop
2000.kms
2000.misc
2000.protocols
2000.realtime.collabo.
2000.setup
2000.transport
2000.win2000
admin
application.conversion
applications
clients
clustering
connectivity
design
development
misc
mobility
setup
tools
  
 
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]

Re:Retrieve attachment from Exchange Server 2003   
hi,
this is fine but i my code,it is not working.i am able to read a file and 
its stored into one folder after i am open the file.its not shows proper 
data on that document using webDav. 
my code:

 Dim _XmlHttp40

 _XmlHttp40 = CreateObject("MSXML2.XMLHTTP.3.0")
_XmlHttp40.open("GET", Url, False, vijayr, passw0rd!)

_XmlHttp40.send()

Dim j As String = _XmlHttp40.responseText

Dim _ResponseBody() As Byte = _XmlHttp40.responseBody

My.Computer.FileSystem.WriteAllBytes("C:\vijayxmlfiles\txt " & FileNameNode, 
_ResponseBody, False)


This folder contains all Attachment files from my outlook.

but its not showing.(for eg: vijaytxt.jpg) its show no preview.

url:http://www.ureader.com/msg/1186800.aspx
date: Fri, 14 Mar 2008 17:45:11 +0800   author:   vijayraghavendran

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us