Monday, June 07, 2004

Importing Entries into Safe Senders, Safe Recipients and Blocked Senders via Script

Outlook 2003 and OWA (Exchange 2003) have the ability to create and maintain White/Black lists that work in conjunction with the IMF to filter Spam.Now that I'm using the IMF to do my spam management theses personal white lists have come up as an issue. Sometimes its necessary to globally white list an address or domain for all users or upload a number of entries into one users whitelist or ensure that as new mailboxes get created any necessary white-list entries get created for users. So a script or programmatic way to do this becomes a bit of a necessity. From a server perspective the lists are all stored in a binary property on a message in the users inbox (junk E-mail rule), so you can use Exoledb and Webdav to get at this property but because its just a binary blob of data decoding and/or adding to it from a script would be quite hard (not that I didn't try).

One solution that I found that works is to re-use some OWA commands in a script using the XML parser (Microsoft.XMLHTTP). Q290591 describes a method you can use to post information to web pages using the XMLHTTP object. What you can do is take this and apply it to OWA, Eg: When you add users to the safe senders list in OWA it posts the following data to the web server.

"cmd=savejunkemailrule"
"addtots=user@domain;"

For Safe Recipients its "addtotr=user@domain;" and for Blocked Senders it "addtojs=user@domain;" (I was so disappointed it wasn't addtobs ).

So to create a script that adds 3 entries to the Trusted Senders list and also enables the junk email filtering check box in OWA the script looks like below.
Note this only works if you aren't using FBA on your mailbox server,

xmlstr = ""
xmlstr = xmlstr & "Cmd=options" & vbLf
xmlstr = xmlstr & "junkemailstate=1" & vbLf
xmlstr = xmlstr & "cmd=savejunkemailrule" & vbLf
xmlstr = xmlstr & "addtots=user@domain;@domain1.com;@domain2.com;"
Set ObjxmlHttp = CreateObject("Microsoft.XMLHTTP")
ObjxmlHttp.Open "POST", "http://yourserver/exchange/yourmailbox/", False, "", ""
ObjxmlHttp.setRequestHeader "Accept-Language:", "en-us"
ObjxmlHttp.setRequestHeader "Content-type:", "application/x-www-UTF8-encoded"
ObjxmlHttp.setRequestHeader "Content-Length:", Len(xmlstr)
ObjxmlHttp.Send xmlstr
Wscript.echo ObjxmlHttp.responseText

7 comments:

Anonymous said...

Did you run this as a log in script? Also, did you have any trouble using this against a secure site ?

Glen said...

I wouldn't recommend you run this as a logon script its for use to add address's as a one of type of thing. Its only a script fragment so you need to develop the idea a bit yourself there is no code to deal with a FBA logon etc (have a look at some of my other sample for this type of thing)

ib said...

I get the following error:
msxml3.dll: Access is denied.
it occurs on ObjxmlHttp.Send xmlstr

Glen said...

You need to look at what authentication your using on your server. EG are you using NTLM or Basic , SSL or FBA?. Does that user that is running the script have rights to the mailbox you trying to run it against. Remember by default if you are trying to use a administrative account that these account are specifically denied access to any other mailbox other then there own see http://support.microsoft.com/kb/q262054/.

If you are using basic authentication then you need to usually hardcode the username and paossword in like

ObjxmlHttp.Open "POST", "http://yourserver/exchange/yourmailbox/", False, "domain\username", "password"

If you using FBA (forms based authentication) then you need to include code to do a syntentic form logon and manage the cookies returned.

Cheers
Glen

Anonymous said...

Hi Glen - Love your site! Thanks for all the great info.

What I'm looking for right now is the ability to run a script and specify the user that I want to add a safe sender to. Any ideas?

I login to my Spam Quarantine and send any good emails on to the recipient. I'd love to be able to add that sender to their list as users rarely do this themselves.

Thanks again! Brent

Glen said...

Hi Brent,

What version of Exchange are you using 2003 or 2007 ? This script will pretty much do what you want eg it will add a user to the safe senders of any account as long as you have the specific rights

Cheers
Glen

Anonymous said...

Hi!

Just found this post and its perfect for what I needed to do.

I'm just working on a VBScript to loop through all the mail enabled users in the AD but thought I'd make a comment that...

If you have secured your site using SSL and the main /Exchange path is using forms base authentication but you are using Outlook Mobile Access and have created a ExchangeVDir for use with this then that virtual directory, in my case /Exchange-OMA has Forms Based Authentication disabled!!

Just change the open command to suit:

ObjxmlHttp.Open "POST", "http://localhost/exchange-oma//", False, "", ""