Exchange Server TechCenter > Exchange Server Forums > 3rd Party Applications > vb.net save attachments from exchange server for a particular mail account
Ask a questionAsk a question
 

Proposed Answervb.net save attachments from exchange server for a particular mail account

  • Wednesday, August 22, 2007 6:26 AMMukeshB Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Thursday, September 27, 2007 7:24 PMJabberBoxer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    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 JayExchange

    o.User = "userName"

    o.Password = "password"

    o.ExtractionDirectory = "C:\ExtrtactedAttachments"

    o.ProcessMail("http://mail.yourExchangeServer.com/exchange/inbox")

    o = Nothing

     

     

    Class:

    ' ---------------------------------------------------------------------

    ' Author: Jerrame Hertz

    ' ---------------------------------------------------------------------

    ' References:

    ' Microsoft XML ver 4.0

    ' ---------------------------------------------------------------------

    Imports System.xml

    Imports System.Xml.Xsl

    Imports MSXML2

    Imports System.io

    Public 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.XMLHTTP40

    Debug.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 Sub

    End Class

    Public 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 Region

    End Class

    • Proposed As Answer byClothering Monday, September 21, 2009 1:46 PM
    •  
  • Monday, October 15, 2007 12:30 PMcoolerboy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    Dear  JabberBoxer ,

    Thank you for sharing with you the code!

  • Wednesday, November 21, 2007 2:52 PMSilfrido Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

     

     

  • Wednesday, February 06, 2008 3:30 AMpadmajyoti Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Tuesday, April 01, 2008 4:08 PMColonel-yum-yum Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Wow! After nearly a week of combing the interwebs finally some code that does what I need it to!
    Thanks

    Colonel
  • Thursday, June 12, 2008 5:57 AMmaduwanthi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

     

  • Friday, October 17, 2008 9:20 PMGyula Gergő Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you, very nice and useful solution, great job.
  • Wednesday, November 19, 2008 2:49 PMsagergaard Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

     

  • Monday, September 21, 2009 1:50 PMClothering Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Thursday, November 05, 2009 3:22 AMSarathi125 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.....