locked
How can I parse an email message? RRS feed

  • Question

  • Hi Guys.

    I've come up with the brainwave to write a small VB application to automatically parse encoded email messages sent from our remote monitoring stations instead of manually dragging each message string to an external parsing application.

    These messages all have the same string format and I can figure out how to check their validity by crossreferencing the senders name, subject heading and message string.

    What I want to achieve is when outlook receives a message, it checks it's from a remote station and not just another email, parses the message string to detect the station ID so it can be moved to a relevant sub directory and to decode the message sting back into standard text that replaces the original string.

    an example...
    [ ][ ][ ][From                      ][Subject                    ][Received                     ][Size ][ ]
                Remote_001.Com    Station_Fault             Day 00/00/0000 00:00  1KB
    message string...
    $DDMMYYYYHHMMSS001002999$

    which would decode into the string...
    On DD/MM/YYYY at HH:MM:SS Station 001 Module 002 Had a 'Fatal Exception Error [999]', please advise service engineer.
    $DDMMYYYYHHMMSS001000999$
    ...then update the original message to match before moving to it's holding directory.

    Inbox            -> Moved from here,
        Station_001 <- into here.
        Station_002
        Station_003

    I've had a look through these forums, but non of the examples seem to work, most seem to be for sending emails, non seem to use the VB editor in Tools->Macro from Outlook.

    Any help on how I can get the data from the mail header and body would be great.

    Also would it be possible for this functionality to run when outlook is running and recieving mail?

    thanks.

    Friday, February 13, 2009 7:35 PM

Answers

  • Alan,

    i have been meaning to do something with my email from one account which i need to send to another account.  Problem is it needs to be certain ones.  I played with this for a while.  this code sample is actually vba but it should get you started.  what i was thinking was to catch the newmail messages in outlook vba and save them to a folder as text files.  you could watch the folder for changes and parse the textfiles to sort them out as needed.  or you could just handle it all in vba.  i personally prefer to work with vb.net as much as possible.  after playing with this vba code for a while i remember trying to code applications in excel vba before.  very awkward compared to vb.net. 

    here is the vba sample

    Private Sub Application_NewMail()
        Dim myolapp As Outlook.Application
        Set myolapp = Outlook.Application
        Dim myExplorers As Outlook.Explorers
        Set myExplorers = myolapp.Explorers
        Dim myFolder As Outlook.Folder
        Dim x As Integer
        Set myFolder = myolapp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
        If myExplorers.Count <> 0 Then
            For x = 1 To myExplorers.Count
                On Error GoTo skipif
                If myExplorers.Item(x).CurrentFolder.Name = "Inbox" Then
                    Dim fldr As Outlook.Folder
                    Set fldr = myExplorers.Item(x).CurrentFolder
                    Dim mail As MailItem
                    Set mail = fldr.Items.GetLast
                    'MsgBox (mail.Subject)
                    mail.SaveAs "c:\test mail\" + mail.Subject + ".txt", olTextOnly
                    'mail.SaveAs "c:\test mail\" + mail.Subject + ".html", olHTML
                    Exit Sub
                End If
    skipif:
            Next x
         End If
         On Error GoTo 0
    End Sub



    you may be able to convert this to vb.net outlook automation as well.  but i figure the main thing is to catch the new mail.  you can get the body of the mail just like the subject.  you can use vb.net to parse the emails after you have them as text.  you can do some searches here in the forum for automation code of word and excel to get the basic idea of how to automate outlook.  i have not tried to automate outlook yet with vb.net so i can't help there off hand.  the code above along with the automation code you find should give you all the clues you need to make it work if you want to go that way.

    hope this helps
    Jeff


    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial
    • Marked as answer by Xingwei Hu Thursday, February 19, 2009 10:17 AM
    Saturday, February 14, 2009 5:24 AM