Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display Sending Server IP from the Header in Outlook Field

Posted on 2009-04-08
9
Medium Priority
?
327 Views
Last Modified: 2012-05-06
Is it possible to add a field in Outlook that will display the sending servers IP address? I am using the SPF check field from Microsoft already. I am looking for something similar. Here's what I want to accomplish:

Emails tagged as spam by GFI on the Exchange Server are forwarded to a designated "SPAM" mailbox to catch any legit emails for fallback purposes. I'd like to sort all emails in this box by the sending servers IP address.
0
Comment
Question by:lewisig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 24097327
Hi, lewisig.

From an Outlook client perspective "yes" that is possible, but it will require some scripting.  A script will have to retrieve the header, parse it for the sending server's IP address, create a user-defined property in the Outlook item, insert the IP address in the property, and save the item.  The script will have to run at the client.  I can help with the script and instructions for using it if you want to go this route.
0
 
LVL 1

Author Comment

by:lewisig
ID: 24097457
Sounds appropriate. Unfortunately, I do not know scripting well so any help would be appreciated.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24098245
No problem.  What version of Outlook do you use?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:lewisig
ID: 24098362
2003 and 2007.  If it has to be one, let's go with 2003
0
 
LVL 76

Accepted Solution

by:
David Lee earned 250 total points
ID: 24120318
Here's the code for doing this.  Follow these instructions to use it.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor
10. Select one or more messages.
11. Run the macro GetSIP

The macro will get the sending server's IP, or at least attempt to (I'll explain that below), and store it in an Outlook user property.  All you'll need to do is modify a view by adding the field to it.  The field name is SendingServer.

I cannot guarantee that the code will get the sending server's IP.  There's nothing unique that marks that IP.  If the header has multiple entries, then it's possible that the code will return the wrong one.
Sub GetSIP()
    Dim olkItem As Outlook.MailItem, olkProp As Outlook.UserProperty
    For Each olkItem In Outlook.Application.ActiveExplorer.Selection
        Set olkProp = olkItem.UserProperties.Find("SendingServer", True)
        If TypeName(olkProp) = "Nothing" Then
            Set olkProp = olkItem.UserProperties.Add("SendingServer", olText, True)
        End If
    Next
    olkProp.Value = GetSenderIP(Outlook.Application.ActiveExplorer.Selection(1))
    olkItem.Save
    Set olkProp = Nothing
    Set olkItem = Nothing
End Sub
 
Function GetSenderIP(olkItem As Outlook.MailItem)
    'Set the error handler to allow us to handle errors in code
    On Error Resume Next
 
    'Declare a constant
    Const CdoPR_TRANSPORT_MESSAGE_HEADERS = &H7D001E
 
    'Declare a few variables
    Dim mapSession As Object, _
        mapMessage As Object, _
        mapFields As Object, _
        strHeader As String, _
        arrHeaders As Variant, _
        varHeader As Variant, _
        intLeftBracket As Integer, _
        intRightBracket As Integer, _
        strIP As String
 
    'Create a CDO session and login to it using the existing Outlook session
    Set mapSession = CreateObject("MAPI.Session")
    mapSession.Logon , , False, False, 0
 
    'Get the selected message's ID
    Set mapMessage = mapSession.GetMessage(olkItem.EntryID, olkItem.Parent.StoreID)
 
    'Get message fields
    Set mapFields = mapMessage.Fields
 
    'Get the Internet header
    Err.Clear
    strHeader = mapFields.Item(CdoPR_TRANSPORT_MESSAGE_HEADERS).Value
    If Err.Number = 0 Then
        arrHeaders = Split(strHeader, vbCrLf)
        For Each varHeader In arrHeaders
            If Left(varHeader, 9) = "Received:" Then
                intLeftBracket = InStr(1, varHeader, "[")
                intRightBracket = InStr(1, varHeader, "]")
                If (intLeftBracket > 0) And (intRightBracket > 0) Then
                    If intRightBracket > intLeftBracket Then
                        strIP = Mid(varHeader, intLeftBracket + 1, intRightBracket - intLeftBracket - 1)
                        If strIP <> "127.0.0.1" Then
                            GetSenderIP = strIP
                        End If
                    End If
                End If
            End If
        Next
    End If
 
    'Logout of the CDO sesison
    mapSession.Logoff
 
    'Clean up
    Set mapSession = Nothing
    Set mapMessage = Nothing
    Set mapFields = Nothing
End Function

Open in new window

0
 
LVL 1

Author Comment

by:lewisig
ID: 24147347
Instead of running a macro on selected messages, is there a method to extract the unique header information to a column for sorting? Can the unigue mark to identify the sending mail server in the message header be the combination of "Received: from" and "by mail.myserver.com"?

examples:

Received: from JSTQIDTOA ([115.138.6.53] RDNS failed) by mail.myserver.com
Received: from host-54-254.compi.net.pl ([194.187.54.254]) by mail.myserver.com
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24153015
"is there a method to extract the unique header information to a column for sorting"
That's exactly what the code is doing.  A column is composed of the values of a given property for each item.  The information you want from the header isn't part of Outlook's object model, i.e. it's not a property.  So the first step is to read each message (each selected message), pull that information from the header, and put it into a user-defined property.  That's what allows you to add a column showing the information.  

"Can the unigue mark to identify the sending mail server in the message header be the combination of "Received: from" and "by mail.myserver.com"?"
Sure.  But how does that help if the message passed through 4 or 5 servers on its way to you?  So long as the server names are all in order and all messages headers are formatted in exactly the same way, then the code should work without any problem.  My point was that none of the servers listed in the header are uniquely marked or tagged as being the server of origin.  
0
 
LVL 1

Author Comment

by:lewisig
ID: 24487672
Never got around to this, sorry. Found a work around.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24487781
I don't mind that you found a workaround, but why give me the lowest grade possible for putting in the effort to help out?
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Know the reasons and solutions to move/import EDB to New Exchange Server. Also, find out how to recover an Exchange .edb file and to restore the file back.
I don't pretend to be an expert at this, but I have found a few things that are useful. I hope that sharing them here will help others, so they will not have to face some rather hard choices. Since I felt this to be a topic of enough importance and…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
how to add IIS SMTP to handle application/Scanner relays into office 365.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question