Calling SpamAssassin Programmatically Using C#
We have recently been developing a program which involves retrieving emails into a ASP.NET system written in C#.
It was quickly realised that we would also need a spam filter to avoid spam messages clogging up the system. Firstly we created a black list and a white list to allow us to pre-approve/disapprove emails from certain addresses.
Next, we needed some way of retrieving a spam score (or probability) for the remaining messages. We looked immpediately to SpamAssassin and easily got it working on our Windows server using the excellent how to guide: Using SpamAssassin with Win32.
However, we did not want the mail to be filtered at the mail server level, we wanted to spam check the messages in our system as they were retrieved and store their spam score.
To do this we created a SpamAssassinWrapper class library in C# (as our system is in ASP.NET). This class simply called “spamassassin.bat” with the email message file as the argument (in its raw format), by starting a new Process (System.Diagnostics.Process).
The Standard Output can be redirected to a StreamReader (see this MSDN article), allowing us to get the results.
We then retrieved each line from the output until we found the “X-Spam-Status” header. The value of the “score” attribute of the header was then retrieved and returned as a nullable float. It is null if for some reason there was no score.
The only problem with this method is that calling “spamassassin” on each individual email is slow. This could be solved by setting up “spamc” and “spamd” on the server and calling this instead as they are C-compiled and much faster, but this looks tricky (on Windows) and for now time is not an important factor.
