Friday, December 16, 2005

Public folder Mail Re-Submit Tool

I came up with this little application this week to solve one annoying problem I have with a small client that uses an anti-spam application that submits any SPAM it finds to a public folder. As with everything the cheaper the anti-spam software the more chance there will be false positives so occasionally there is a need to go though this public folder and forward any false positives on to the intended receiver. Using the normal Outlook or OWA method will change the reply address’s and also the subject line which is usually also altered by the anti spam program. So what I came up with was two simple ASP pages that could be used to firstly query all the mail in a public folder and then allow an export (which is sometimes useful for checking the X-headers added by this spam program) and also resubmit an email to a desired email address (and allow any fix-up to the subject) buy firstly getting the RFC822 stream of the message via CDOEX and then saving this stream with an added x:sender and x:receiver field to the pickup directory on the server.

The Resubmit Trick

I picked up this resubmit trick by basically copying what the IMF and IMF archive managers do. If you have UCE Archiving enabled with IMF it saves a copy of any SPAM it detects to a directory and writes the sender and reciver address’s as x:sender and x:receiver at the top of the file. If these two fields are at the top of the file when you put a mail into the pickup directory Exchange will process these as the envelope fields and only attempt to deliver the message to the x:receiver address (and not the To and CC fields of the message). I started out thinking I could do this by adding the X-header’s via the fields collection in CDOEX like I usually do which works but created a problem that because using the procedure for some reason added another field to the top of the file before x:sender and x:receiver when exchange processed the email that was placed in the pickup directory it would also try and resend the email to any of the address’s that where in the To and CC field of the message (which is a really bad thing).To fix this problem I ended up getting the stream of the message appending to the front of the steam and then writing it to disk which made sure that these two fields where always at the top of the file

Using the tool

The two asp pages that make up this tool use Exoledb as the underlying mechanism to access the Exchange Store. This means that the pages must be run locally on an Exchange server that has an instance of the public folder you want to access. To run the tool it needs to be put into a directory usually under the default website (eg create a directory under wwwroot called resub) that has NTLM authentication enabled (via IIS admin). Because this was a really simple tool I’ve also hard coded the Pickup directory in the ASP file so if you have the pick directory set to any other directory other then “c:\Program Files\Exchsrvr\Mailroot\vsi 1\PickUp” you need to change the following line in expresub.asp file

dim rec,oCon,Href,msgobj,resub,Subject,Toaddress,pickupdirectory,rfcmsg
pickupdirectory = "c:\Program Files\Exchsrvr\Mailroot\vsi 1\PickUp"

This tool is not just limited to public folders if you have a mailbox you want to re-submit messages from it will also work just enter in the OWA url to the inbox you want (eg: http://servername/exchange/username/inbox ) and press submit. The main page of this application is called resubmain.asp so if you have placed the files in a directory called resub under wwwroot you should be able to access the tool by using http://servername/resub/resubmain.asp .

Limitations

One limitation is that you can only submit a message to one email address and you should only resubmit messages to internal address resubmitting to external address may cause unexpected results.

Changing Export format

Currently the export format is set to .txt files this is mainly because I wanted to be able to view the message in notepad. If you wanted to export the message as a .eml just change the extension in the following line in resubmail.asp and you will then be able to open the message in Mail client such as Outlook Express

Response.AddHeader "Content-Disposition","attachment;filename=export.txt"

Change to

Response.AddHeader "Content-Disposition","attachment;filename=export.eml"

Download

I’ve put a download of the ASP pages here

12 comments:

Daniel Moore said...

Excellent ! I have already added it to my site. Would it be possible to pre-fill the "To email address" in the resubmit section and then delete the email from the "Spam Bin" once it has been resent ?

Glen said...

To Automatically fill in the To email address you can change the following line in the expresub.asp file

response.write "<p>Message To :<input type=""text"" name=""Toaddress""
size=""80"" value=""" & mid(msgobj.to,instr(msgobj.to,"<")+1,(instr(msgobj.to,">")-1)-instr(msgobj.to,"<"))
& """></p>"

To delete the email you need to add somethink like this after the following line

stm.savetofile pickupdirectory & "\" & day(now) & month(now) & year(now) & hour(now) & minute(now) & rndval & ".eml"

set msgobj = nothing
rec.open cstr(Href),oCon,3
rec.deleterecord
rec.close

Daniel Moore said...

Glen,

You are an .ASP code God !!!

Thanks.

Anonymous said...

Ok so we can delet the item..
Excelent.. How would one move it (say to a 'done' sub-folder)

David

Daniel Moore said...

Glen,

I am working on a project to move spam from a generic public folder to a folder called spam."users alias".

My question is: How do you get the user alias from the email address ?

I am using your public folder re-submit tool as a basis and i can get it to send the mail to a specified folder (i.e. spam.test) but i would like to send it to a specific public folder.

Thanks for all your help.

Dan.

Glen said...

Hi Daniel,

To get the alias from the email address you should be able to use the Iperson interface

Set Person = CreateObject("CDO.Person")
strURL = "mailto:email@domain.com"
Person.DataSource.Open strURL
wscrpt.echo Person.fields("http://schemas.microsoft.com/exchange/alias").value

The other way would be to do a search of active directory based on the email address.

Cheers
Glen

Glen said...

Hi David

To Move an email instead of deleterecord you can use moverecord to move the email within a mailbox.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_clb_moving_copying_using_ado_vb.asp

Daniel Moore said...

Glen,

Thanks a lot. I've got it working now to seperate the sapm into user specific folders. Obviously, it only processes the first email address and not the CC or BCC yet (I don't think I even want to go there !!!)

Once again, thaks for all your help

Joe said...

What if I wanted to export the messages from the public folder to a PST?

Glen said...

You need to use Mapi to access a PST so you need a wrapper like CDO 1.2. What i would suggest is you look at using RDO which is part of Redemption http://www.dimastr.com/redemption/ . I wrote a export script for the dumpster which you should be able to adapt http://gsexdev.blogspot.com/2006/03/export-dumpsters-deleted-items-from.html

Chris's Group said...

Thanks for your tool!! Its awesome!!!

Know of anyway to make it repost to the "list" of emails" instead of going back to the url page each time?

Also is there a way to make it automatically strip certain parts of the subject (Like mine start with "emailaddress - Spam Reason - Real Subject")

Glen said...

Its just simple asp you should be able to do this using a simple redirect either Javascript of something else.

To do the subject line you would need to add a line such as

msgobj.fields("urn:schemas:httpmail:subject") = replace(msgobj.fields("urn:schemas:httpmail:subject"),"Text to replace","")