How to import a list of IP addresses into a Microsoft SMTP relay server

This vbscript will allow you to import a list of IP addresses to a Windows (IIS6) SMTP relay server.

1. To use the script save it as ImportRelayList.vbs

2. Save your ip.txt file in the same folder as the script

3. Store IP addresses in ip.txt – the format of each line should be IP,MASK

4. Run cscript ImportRelayList.vbs to run the script

Option Explicit
Dim objSMTP,objRelayIpList,objCurrentList,objIP,objFSO,objTextFile,count,newIpList(),inputOption
Set objSMTP = GetObject("IIS://localhost/smtpsvc/1")
Set objRelayIpList = objSMTP.Get("RelayIpList") 
'objRelayIpList is of type IIsIPSecuritySetting http://msdn.microsoft.com/en-us/library/ms525725.aspx
Wscript.Echo "============================================" 
Wscript.Echo "CURRENT SETTINGS"
Wscript.Echo "================"
Wscript.Echo " " 
Wscript.Echo "Computer(s) that may relay through this virtual server."
Wscript.Echo " " 
' GrantByDefault returns 0 when "only the list below" is set (false) and -1 when all except the list below is set(true)
If objRelayIpList.GrantByDefault = true Then
    Wscript.Echo "All except the list below :"
    objCurrentList = objRelayIpList.IPDeny
Else 
    Wscript.Echo "Only the list below :"
    objCurrentList = objRelayIpList.IPGrant
End If
count = 0
For Each objIP in objCurrentList
        Wscript.Echo objIP 
        count = count + 1
Next
If count = 0 Then
    Wscript.Echo "*NIL*"
End If
Wscript.Echo "============================================" 
Wscript.Echo " " 
Wscript.Echo "Replacing ReplayIpList with the IP address(es) from the ip.txt file."
Wscript.Echo " "
Do While Not((inputOption = "a") Or (inputOption = "d") Or (inputOption = "x") ) 
Wscript.Echo "ENTER " 
Wscript.Echo "A to add to Allow List (Only the list below)"
Wscript.Echo "D to add to Deny List (All except the list below)"
Wscript.Echo "X Exit without making changes"
Wscript.Echo " "
inputOption = lcase(trim(Wscript.StdIn.ReadLine))
Loop
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("ip.txt") Then
    Set objTextFile = objFSO.OpenTextFile("ip.txt",1)

    count = 0
    Do Until objTextFile.AtEndOfStream
        Redim Preserve newIpList(count)
       newIpList(count) = objTextFile.Readline
        count = count + 1
    Loop
    objTextFile.Close

    'For each objIP in newIpList
    '    Wscript.Echo objIP
    'Next
    Wscript.Echo " "
    Select Case inputOption
        Case "a"
            objRelayIpList.GrantByDefault = false
            objRelayIpList.IpGrant = newIpList
            Wscript.Echo "SET " & count &" address(es) to Allow List"        
        Case "d"
            objRelayIpList.GrantByDefault = true
            objRelayIpList.IpDeny = newIpList
            Wscript.Echo "SET " & count &" address(es) to Deny List"
        Case "x"
            Wscript.Echo "Exiting without making changes"
            Wscript.Echo "============================================" 
            Wscript.Quit
    End Select
    
    objSMTP.Put "RelayIpList",objRelayIpList
    objSMTP.SetInfo
    Wscript.Echo " "
    
    Wscript.Echo "============================================" 
Else
    Wscript.Echo "Please create a file ip.txt that contains the list of IP address(es)"
    Wscript.Echo "FORMAT : Each Line should be IP,MASK "
    Wscript.Echo "EX     : 127.0.0.1,255.255.255.255"
End If

7 Replies to “How to import a list of IP addresses into a Microsoft SMTP relay server”

  1. Love the script but I’m trying to run it on an IIS 10 server. It complains on the objRelayIpList.IpGrant = newIpList, gives me “Microsoft VBScript runtime error: Invalid procedure call or argument”.

  2. Was looking all over for this via PowerShell! VBS!! One question, I don’t see the limitation in this script but it I can only import a list of 50 lines and since the list is replaced each time… Currently Googling for ‘how many IPs can I have in the relay list’.

  3. I’m getting the following error when I run the script:

    C:\Scripts\ImportRelayList.vbs(59, 13) Microsoft VBScript runtime error: Invalid procedure call or argument

    could you tell me what i’m missing to make it work?

  4. Getting same error – C:\Scripts\ImportRelayList.vbs(59, 13) Microsoft VBScript runtime error: Invalid procedure call or argument

    1. I was having the same error as others and figured out the issue…

      “C:\Scripts\ImportRelayList.vbs(59, 13) Microsoft VBScript runtime error: Invalid procedure call or argument”

      We found we inadvertently had duplicate IPs listed in the import file… once we cleaned up the duplicates the app ran successfully again.

      Hopefully, this helps others as we spent more time than needed trying to figure out this error!

  5. still getting “C:\Scripts\ImportRelayList.vbs(59, 13) Microsoft VBScript runtime error: Invalid procedure call or argument” here even with no duplicates in the ip.txt file

Leave a Reply

Your email address will not be published. Required fields are marked *