Link to home
Start Free TrialLog in
Avatar of John-D-Chapman

asked on

Script to change POP and SMTP Servers in Outlook 2002

My company is going to be switching ISP's in the near future. In preparation for this, I am researching whether changing the incoming (POP) and outgoing (SMTP) servers in Outlook 2002 on each domain workstation can be scripted (using simple batch).

Ideally, this code would run as a section of the user's login script, meaning that once implemented, the code would run on logon and change the server addresses of the default mail account in outlook 2002.

I can find the data in the registry under "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles", but it is binary data and I'm not sure if simple batch can convert the new server addresses to binary in order to merge them into their correct keys int he registry.

Or I may already be going way off base. I'm not sure.

If simple batch methods cannot be used, are there any other options that anyone would care to hypothesize about?
Avatar of AmazingTech

You may be able to create a custom prf file to import into Outlook to change your settings.

Take a look at this.
Avatar of John-D-Chapman



Thanks for the reply. I had looked into the PRF files.

However, they seem to have an overkill of information in them for someone wanting to simply change a single pop and smtp server address. I tried to find info on whether or not sections and variables could be omitted from a PRF file, but have not been able to come up with an answer either way.

I don't believe one can import a PRF file that only includes specific variables within each section, or with sections omitted.

For example, I would want a PRF file that only includes sections 1, 3 and 5. Within those sections, I would only want to specify certain variables, not bothering with any variables that I don;t want, or need to touch.
Hmm... When I have a chance I'll continue to try and see if I can get the prf to work.

I do think you can do it with a simple batch modifying the binary value to the pop and smtp server.

I will work on a batch file soon when I get a spare moment. How soon are you changing ISPs?
Avatar of AmazingTech

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Fantastic!! This works perfectly.

It feels good to now know and understand that:
  • The REG_BINARY value (Hexadecimal) to be added (with REG_ADD) has to include the "00" pairs that separate each HEX pair in the registry entry, as well as three "00" pairs at the end of the address.
For example, adding 706f702e637261702e6e6574 (the hex value of a bogus server) to the registry, and then checking the setting in Outlook, you would see an unexpected and incorrect result, since it should have been added like this (just as you specified with the values in your script):
  • 70006f0070002e0063007200610070002e006e00650074000000
I did tweak your code a wee bit, simply out of preference (so my feeble brain could see and interpret the code better). Candidate for the finished script is attached (will not roll out until tested more). Currently no rush anyway, as our switch is not imminent at the moment.

Apologies for taking so long to respond to your first reply (about the PRF files).

I'm a little more educated now, and you are definitely a GURU!

Thank you so much for your efforts!!! I only wish I could give you more points than I am able to.

@ECHO OFF & setlocal enableextensions
:: Current POP3 Server without separators = 706f702e67656f726769616e2e6e6574
:: Here is the current POP3 address complete with separators.
@Set OldPOP=70006F0070002E00670065006F0072006700690061006E002E006E00650074000000
:: New POP3 server without separators = 706f702e637261702e6e6574
:: With separators:
@Set NewPOP=70006f0070002e0063007200610070002e006e00650074000000
:: Current SMTP Server without separators = 736d74702e67656f726769616e2e6e6574
:: With separators:
@Set OldSMTP=73006D00740070002E00670065006F0072006700690061006E002E006E00650074000000
:: Possible current Alternate SMTP Server without separators = 736d7470322e67656f726769616e2e6e6574
:: With separators:
@Set OldSMTP2=73006D007400700032002E00670065006F0072006700690061006E002E006E00650074000000
:: New SMTP Server withour separators = 736d74702e637261702e6e6574
:: With separators:
@Set NewSMTP=73006d00740070002e0063007200610070002e006e00650074000000
for /f "tokens=2,*" %%a in ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" ^| Find /i "DefaultProfile"') do Set DefaultProfile=%%b
echo Setting mail servers for the following Outlook profile: %DefaultProfile%
for /f "tokens=*" %%a in ('reg query "hkcu\software\microsoft\windows nt\currentversion\windows messaging subsystem\profiles\%DefaultProfile%\9375CFF0413111d3B88A00104B2A6676" ^| find /i "9375CFF0413111d3B88A00104B2A6676\"') do (
	for /f "tokens=*" %%c in ('reg query "%%a" /v "POP3 Server" ^| FIND /i "POP3 Server"') do (
		echo %%c | find /i "%OldPOP%"
			REG ADD "%%a" /v "POP3 Server" /t REG_BINARY /f /d %NewPOP%
			echo New POP3 Address added.
		) ELSE (
			echo Current POP3 address does not match address to be changed.
	for /f "tokens=*" %%d in ('reg query "%%a" /v "SMTP Server" ^| FIND /i "SMTP Server"') do (
		echo %%d | find /i "%OldSMTP%"
			REG ADD "%%a" /v "SMTP Server" /t REG_BINARY /f /d %NewSMTP%
			echo New SMTP Address added.
		) ELSE (
			echo Current SMTP address does not match address to be changed. Checking for alternate SMTP address...
	for /f "tokens=*" %%e in ('reg query "%%a" /v "SMTP Server" ^| FIND /i "SMTP Server"') do (
		echo %%e | find /i "%OldSMTP2%"
			REG ADD "%%a" /v "SMTP Server" /t REG_BINARY /f /d %NewSMTP%
			echo New SMTP Address added in place of old alternate SMTP Address.
		) ELSE (
			echo Alternate SMTP address not found in that entry.

Open in new window

A whole lot of awesome!
That's great that you were able to modify it to your liking. Saves me alot of work and back and forth.

Binary values work the same way as REG_MULTI_SZ values in that each binary value/multi string line is terminated or separated by a null character (0). To signal the end of values they are terminated with 2 null characters but a third null character is also required to terminate the 2 null characters.

Don't worry about the amount of points I really don't do this for the points. I just like solving problems.

I'm still interested in getting the prf file to work but I think it's going to be a headache if all your profiles and account names are not all the same.  With this batch script atleast it's dynamic and will not require these exact names just the profile being the default profile.

We could change this script to go through all profiles though.
I absolutely need the dynamic nature of a script like this, because yes: all profiles and accounts would be different.

Yup, this script could easily be modified to iterate through all profiles, not just the default.

Thanks for explaining about the null characters to separate the binary values.

Thanks again, and much appreciated. Honestly, in all my googling about trying to make this simple pop/smtp server change, this is the first simple alternative I've found to the PRF file approach.

Hopefully this question/answer will soon end up in web search results for any future folks trying to accomplish the task.