Asked by:
Saving Attachments from Exchange 2003 to Local File System Using WebDEV and VB.NET

Question
-
User1008945111 posted
I'm trying to download the attachments related to any given email in a specified users inbox and save them to the local file system. I have it inserting the message body, subject and sender's address into a database, then if there are any attachments, I get the list of attachments and do a get on each file path, returning the file content. From what I have researched the file content is returned to me as binary data, which I then need to stream to a created file on the server.
So below is my attempt at streaming the data and saving the file, I think I am close, as it does create the file, and there is a bunch of illegible stuff in it if I open it with a text editor. It's not right though, the file that is saved is not valid. If I try to save an excel file for example, excel just tells me there it is not a recognized format. Images opened with MS fax viewer gives me "No Preview Available". Some guidence would be greatly appreciated.
Imports MSXML2 Imports System.xml Imports System.Xml.Serialization Imports System Imports System.IO Imports System.Data Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myConnection As SqlConnection Dim myCommand As SqlCommand myConnection = New SqlConnection("server=Groundnet;uid=***;pwd=***;database=Current;") Dim xmlhttp As New MSXML2.ServerXMLHTTP40 xmlhttp.open("PROPFIND", "http://192.168.0.246/exchange/ts/inbox", False, "ts", "****") xmlhttp.setRequestHeader("Depth", "1") xmlhttp.setRequestHeader("Content-type", "xml") xmlhttp.send() Dim xmlstr As Object = xmlhttp.responseXML Dim mails As Object = xmlstr.getElementsByTagName("a:response") Dim i As Integer For i = 0 To mails.length - 1 Dim m_subject = mails(i).getElementsByTagName("d:subject") If m_subject.length <> -1 Then Dim m_body = mails(i).getElementsByTagName("e:textdescription") Dim m_sender = mails(i).getElementsByTagName("e:fromemail") Dim m_eml = mails(i).getElementsByTagName("a:href") Dim atch = mails(i).getElementsByTagName("e:hasattachment") Dim atch_flg As Integer Dim j As Integer For j = 0 To m_body.length - 1 m_body = m_body(j).firstChild.data() m_subject = m_subject(j).firstChild.data() m_sender = m_sender(j).firstChild.data() m_eml = m_eml(j).firstChild.data() myConnection.Open() myCommand = New SqlCommand("INSERT INTO EmailTable (Mail_Subject,Mail_body,Sender) VALUES('" & Replace(m_subject, "'", "''") & "','" & Replace(m_body, "'", "''") & "','" & m_sender & "')", myConnection) myCommand.ExecuteNonQuery() myConnection.Close() atch = atch(j).firstChild.data atch_flg = atch If atch_flg = 1 Then xmlhttp.open("X-MS-ENUMATTS", m_eml, False, "ts", "****") xmlhttp.setRequestHeader("Content-Type", "xml") xmlhttp.setRequestHeader("Depth", "1") xmlhttp.send() xmlstr = xmlhttp.responseXML Dim at_list = xmlstr.getElementsByTagName("a:response") Dim k As Integer For k = 0 To at_list.length - 1 If at_list(k).getElementsByTagName("a:href").length <> -1 Then Dim fpath As String = at_list(k).getElementsByTagName("a:href")(0).firstChild.data() Dim fname As String = at_list(k).getElementsByTagName("e:attachmentfilename")(0).firstChild.data() Dim ftype As String = at_list(k).getElementsByTagName("d:x3703001f")(0).firstChild.data() xmlhttp.open("GET", fpath, False, "ts", "****") xmlhttp.send() Dim xmlstr2 As String = xmlhttp.responseText Dim fs As New FileStream("C:\" & fname, FileMode.Create) Dim w As New BinaryWriter(fs) w.Write(xmlstr2) w.Close() fs.Close() MsgBox("") End If Next End If Next End If Next End Sub End Class
Thanks,
Basscyst
Thursday, May 3, 2007 9:04 PM
All replies
-
User599111684 posted
Hi did you ever get anywhere with this as it's exactly what I'm trying to do?
Thanks
Chris
Tuesday, August 12, 2008 10:23 AM -
User-252101379 posted
Try this
Replace
================
Dim xmlstr2 As String = xmlhttp.responseText
Dim fs As New FileStream("C:\" & fname, FileMode.Create)
Dim w As New BinaryWriter(fs)
w.Write(xmlstr2)
w.Close()
fs.Close()==================
By
=================
Dim _ResponseBody() As Byte = xmlhttp.responseBody
My.Computer.FileSystem.WriteAllBytes("C:\Attachments\" & fname, _ResponseBody, False)==================
Wednesday, August 20, 2008 3:37 AM