WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Web UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get SMTP Mail Server


Here is some code that attempts to track down the SMTP mail server in the registry.

#DefineFunction Hex2Dec(hex)
   str="0123456789ABCDEF"
   hex=StrTrim(StrUpper(hex))
   hexlen=StrLen(hex)
   dec=0
   for x=1 to hexlen
       dec=(dec*16) + StrIndex(str,strsub(hex,x,1),0,@fwdscan) -1
   next
   return(dec)
#EndFunction


keypath = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676"
if !RegExistKey(@REGCURRENT,keypath)
   Message("Errror"<"Registry key doesn't exist")
   exit
endif

key=RegOpenkey(@REGCURRENT, keypath)
keylist=RegQueryKeys(key)


For ii = 1 to ItemCount(keylist,@tab)
    acctname = ""
    smtpserver = ""
    keyname = ItemExtract(ii,keylist,@tab)
    If RegExistValue(key, StrCat(keyname, "[SMTP Server]")) && RegExistValue(key, StrCat(keyname, "[Account Name]"))
        data = RegQueryBin(key, StrCat(keyname, "[Account Name]"))
        
        ;Convert hex bin data to ansi string
			For x = 1 to ItemCount(data," ")
				hex = ItemExtract(x,data," ")
				if hex == "00" then continue
				dec = Hex2Dec(hex)
				char = Num2Char(dec)
				acctname = StrCat(acctname,char)
			Next


        data = ""
        data = RegQueryBin(key, StrCat(keyname, "[SMTP Server]"))
        ;Convert hex bin data to ansi string
			For y = 1 to ItemCount(data," ")
				hex = ItemExtract(y,data," ")
				if hex == "00" then continue
				dec = Hex2Dec(hex)
				char = Num2Char(dec)
				smtpserver = StrCat(smtpserver,char)
			Next

        Message(StrCat("Account Name: ",acctname), StrCat("SMTP Server: ", smtpserver))
    Endif

Next

RegCloseKey(key)


Note: Thsis example requires the Reggie Extender.

Code tested on Windows XP, 2000 and Windows 95

#DefineFunction Hex2Dec(hex)
   str="0123456789ABCDEF"
   hex=StrTrim(StrUpper(hex))
   hexlen=StrLen(hex)
   dec=0
   for x=1 to hexlen
       dec=(dec*16) + StrIndex(str,strsub(hex,x,1),0,@fwdscan) -1
   next
   return(dec)
#EndFunction


#DefineFunction GetSMTPServer()
	AddExtender("WWREG34I.DLL")

	srchstr="SMTP Server"  
	topkey=@REGCURRENT    
	topsub="\"
	looktype=0             
	lookat=7       
	dosubtree=@FALSE        
	smtppathlist=rRegSearch(topkey,topsub,srchstr,looktype,lookat,dosubtree)
	if smtppathlist == ""
		Message("Error",StrCat(srchstr," Not found in registry"))
		return smtppathlist
	endif
   smtpservers = ""
   For x = 1 to ItemCount(smtppathlist,@Tab)
      smtppath = ItemExtract(x,smtppathlist,@Tab)
   	;remove leading '\'
   	if StrSub(smtppath, 1, 1)=="\" 
   		 smtppath = StrSub(smtppath, 2, -1)
   	endif
   	
   	;Read data from registy
   	datatype = RegEntryType(topkey,smtppath)
   	data = RegQueryEx (topkey, smtppath, "", datatype)
   	smtpserver = ""
   	Switch datatype
   		case 3 ; REG_BINARY 
   			;Convert hex bin data to ansi string
   			For i = 1 to ItemCount(data," ")
   				hex = ItemExtract(i,data," ")
   				if hex == "00" then continue
   				dec = Hex2Dec(hex)
   				char = Num2Char(dec)
   				smtpserver = StrCat(smtpserver,char)
   			Next
   		break
   		case datatype  ;Any other REG_ type
   			smtpserver = data
   		break
   	EndSwitch

      if x == 1 then smtpservers = smtpserver
      else smtpservers = StrCat(smtpservers,@tab,smtpserver)
   Next
   return smtpservers
#EndFunction




smtpserver = GetSMTPServer()
if smtpserver == ""
  smtpserver = ("My Emailer", "Please enter the name of your SMTP Server", "")
endif
Message("SMTP Server",smtpserver)
exit



Article ID:   W16229
File Created: 2005:01:14:10:48:40
Last Updated: 2005:01:14:10:48:40