vb.net save attachments from exchange server for a particular mail account
Dear All,
I am trying to resolve the following business issue we have:
Currently our administrator recieves e-mails with attachments from a fixed e-mail address in his e-mail mailbox twice a daily; he saves the attachment from this address locally on his system and then uploads onto the web server using the .net file upload component.
To get over from this manual work, I plan to automate the process by implementing a scheduled VB.net application (runs twice a day at 7) to read mails "from address" on the exchange server, download the attachments from this address, save it locally on the server, send a path to the SQL server database and using WebDav ut up the attachments onto a remote FTP server. Therefore any visitors accessing the web page will see the attachments without having someone to manually put up the files. Please note there is a fixed "from address" and a fixed "toaddress"
If the above is right, can you help me out with code snippets to fasten the process.
Thanks
All Replies
I figured out a bit more since I first replied to this post, you can now extract Excel and Word documents without corrupting them.
Best of luck with this.
Use:
Dim o As New JayExchangeo.User =
"userName"o.Password =
"password"o.ExtractionDirectory =
"C:\ExtrtactedAttachments"o.ProcessMail(
"http://mail.yourExchangeServer.com/exchange/inbox")o =
NothingClass:
' ---------------------------------------------------------------------
' Author: Jerrame Hertz
' ---------------------------------------------------------------------
' References:
' Microsoft XML ver 4.0
' ---------------------------------------------------------------------
Imports
System.xmlImports
System.Xml.XslImports
MSXML2Imports
System.ioPublic
Class JayExchange Inherits Base Private _User As String Private _Password As String Private _ExtractionDirectory As String Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then ' End If MyBase.Dispose(disposing) End Sub Public WriteOnly Property User() As String Set(ByVal value As String)_User = value.ToString
End Set End Property Public WriteOnly Property Password() As String Set(ByVal value As String)_Password = value.ToString
End Set End Property Public Property ExtractionDirectory() As String Get Return _ExtractionDirectory.ToString End Get Set(ByVal value As String) If Microsoft.VisualBasic.Right(value.ToString, 1) = "\" Then_ExtractionDirectory = value.Substring(1, value.Length - 1)
Else_ExtractionDirectory = value.ToString
End If End Set End Property Public Sub ProcessMail(ByVal MailURL As String, Optional ByVal XmlNode As System.Xml.XmlNode = Nothing) Dim _XmlHttp40 As New MSXML2.ServerXMLHTTP40 Dim _XmlDataDocument As New System.Xml.XmlDataDocument Dim _XmlNodeList As XmlNodeList = Nothing Dim _XmlNode As System.Xml.XmlNode = Nothing Dim _ResponseText As String = "" Dim iNode As Integer = 0 ' Open exchange and request list of emails from a directory._XmlHttp40.open(
"PROPFIND", MailURL.ToString, False, _User.ToString, _Password.ToString)_XmlHttp40.setRequestHeader(
"Depth", "1")_XmlHttp40.setRequestHeader(
"Content-type", "xml")_XmlHttp40.send()
' Store the data and open it in an xml document object._ResponseText = _XmlHttp40.responseText
_XmlDataDocument.LoadXml(_ResponseText)
' Get a list of email nodes._XmlNodeList = _XmlDataDocument.GetElementsByTagName(
"a:href") On Error Resume Next ' Loop through the nodes and process each email. For iNode = _XmlNodeList.Count - 1 To 0 Step -1_XmlNode = _XmlNodeList.Item(iNode)
ProcessEmail(_XmlNode)
Next_XmlHttp40 =
Nothing_XmlDataDocument =
Nothing_XmlNodeList =
Nothing_XmlNode =
Nothing End Sub Private Sub ProcessEmail(ByVal _XmlNode As System.Xml.XmlNode) Dim _XmlHttp40 As New MSXML2.ServerXMLHTTP40 Dim _XmlDataDocument As New System.Xml.XmlDataDocument Dim _XmlNodeList As XmlNodeList = Nothing Dim _XmlAttachmentNameList As XmlNodeList = Nothing Dim _XmlAttachmentNode As System.Xml.XmlNode = Nothing Dim _XmlAttachmentNameNode As System.Xml.XmlNode = Nothing Dim _ResponseText As String = "" Dim iNode As Integer = 0_XmlHttp40.open(
"X-MS-ENUMATTS", _XmlNode.InnerText, False, _User.ToString, _Password.ToString)_XmlHttp40.setRequestHeader(
"Depth", "1")_XmlHttp40.setRequestHeader(
"Content-type", "xml")_XmlHttp40.send()
' Store the data and open it in an xml document object._ResponseText = _XmlHttp40.responseText
_XmlDataDocument.LoadXml(_ResponseText)
' Get a list of attachment nodes._XmlNodeList = _XmlDataDocument.GetElementsByTagName(
"a:href")_XmlAttachmentNameList = _XmlDataDocument.GetElementsByTagName(
"e:attachmentfilename") ' Loop through the nodes and process each email. For iNode = _XmlNodeList.Count - 1 To 0 Step -1_XmlAttachmentNode = _XmlNodeList.Item(iNode)
_XmlAttachmentNameNode = _XmlAttachmentNameList.Item(iNode)
ExtractAttachment(_XmlAttachmentNode, _XmlAttachmentNameNode)
Next_XmlHttp40 =
Nothing_XmlDataDocument =
Nothing_XmlNodeList =
Nothing_XmlNode =
Nothing End Sub Private Sub ExtractAttachment(ByVal _XmlNode As System.Xml.XmlNode, ByVal FileNameNode As System.Xml.XmlNode) Dim _XmlHttp40 As MSXML2.XMLHTTP40Debug.Print(_XmlNode.InnerText)
_XmlHttp40 =
New MSXML2.XMLHTTP40_XmlHttp40.open(
"GET", _XmlNode.InnerText, False, _User.ToString, _Password.ToString)_XmlHttp40.send()
Dim _ResponseBody() As Byte = _XmlHttp40.responseBody My.Computer.FileSystem.WriteAllBytes(_ExtractionDirectory & "\" & FileNameNode.InnerText, _ResponseBody, False)_XmlHttp40 =
Nothing End SubEnd
ClassPublic
Class Base Implements IDisposable Private disposedValue As Boolean = False ' To detect redundant calls ' IDisposable Protected Overridable Sub Dispose(ByVal disposing As Boolean) If Not Me.disposedValue Then If disposing Then ' TODO: free unmanaged resources when explicitly called End If ' TODO: free shared unmanaged resources End If Me.disposedValue = True End Sub#
Region " IDisposable Support " ' This code added by Visual Basic to correctly implement the disposable pattern. Public Sub Dispose() Implements IDisposable.Dispose ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.Dispose(
True)GC.SuppressFinalize(
Me) End Sub#
End RegionEnd
Class- Proposed As Answer byClothering Monday, September 21, 2009 1:46 PM
Dear JabberBoxer ,Thank you for sharing with you the code!
Hello,
Thanks for share your code it is a good approach to what I need, but I'd like to know how can you associate the email attachment with email content (body, subject, etc); so it can be recorded in a database.
Thanks in advance,
Silfrido
Dear All,
I am trying to save zip file on database to pick data type binery. but i am unable to save it. so please every body know about it please reply me with code for ado.net.
thanks
padmasundar kafle
programmer
Akhil systems pvt. ltd.
wwww.akhilsystems.com
- Wow! After nearly a week of combing the interwebs finally some code that does what I need it to!
Thanks
Colonel Dear All,
I am trying to get "FROM" address from the email that I downloaded using the "JaExchange". Can you please tell me how I can do this.
Thanks
- Thank you, very nice and useful solution, great job.
This is exactly what I was looking for. Thank you very much.
But I have one question. I would also like so save the body of the mail to a text-file. How can the code be extended to do that too?
Thanks again
sagergaard
- Hi jabberBoxer
I see this thread was done in 2007..... quite sometime ago.... It has taken me abotu a week to try and find something, and your assistance did EXACTLY the job I wanted!!!! I thank you greatly for this assistance!!!!!
I just have 1 question..... I would like to some how mark the Emails "ID's" and save them , so that the next time the application is run, it will read through these "ID's" and see which emails have already been read(those which attachments were saved) - This is basically to read ONLY the mails which have note yet been read...
Is this possible ? And how difficult is it ? - I did the same thing using Chilkat software, so I know it is possible.... but have no idea how....
Could you assist with this ???
Thanks once again for displaying your code...
Regards,
Clothering - Hi JabberBoxer,
I need to develop a mail program in vb6, I done upto alert for new mails, But I cant find the new mail and save the attachments in to a system directory,
I think your class may be useful for me. But I need to know is it possible to use this class with VB6 as a dll.
If we can use in the sense then I need to know that how to convert or make it as a common dll to all programs.
And I need the class in C#, I think it will be better for me.
Thanks.....


