What is spamd and spamc?
Let's start with SpamAssassin. SA is probably the world's best software for filtering of spam. But it is written in Perl and that means slow, especially on Win32 platform. So, there is a little help, called 'spamd'. It's a daemon (UN*X equivalent for 'service') allowing to communicate with SA over network and limiting cost of calling SA. And spamc is a client component for this daemon.
This means that you can run some Unix-like OS on a separate (or maybe even only virtual) machine, and run SpamAssassin and spamd there. Then, from your Windows-based mail server, everything that must be done is to call the spamd using spamc, and process results.
Well, the communication protocol is very simple and is described in SpamAssassin documentation. In short, you need to open TCP connection to port where spamd lives (usually 783), and then send command header (i.e.,
PROCESS SPAMC/1.0) and then RFC822-formatted source of message. Then you must drop the sending connection.
spamd would process the message with SpamAssassin and send the modified result in the same fashion.
Implementation under .NET
.NET implementation is very simple, with one exception: due to need for one-sided connection shutdown, we cannot use
System.Net.Sockets.TcpClient. We must go one level down and use directly the socket communication.
The simple and dirty proof-of-concept code may look like:
Dim SB As New System.Text.StringBuilder
SB.Append("\nSubject: Test spam mail (GTUBE)")
SB.Append("\nDate: Wed, 23 Jul 2003 23:30:00 +0200")
SB.Append("\nFrom: Sender <email@example.com>")
SB.Append("\nTo: Recipient <firstname.lastname@example.org>")
SB.Append("\nContent-Type: text/plain; charset=us-ascii")
SB.Append("\nIf your spam filter supports it," & _
" the GTUBE provides a test by which you")
SB.Append("\ncan verify that the filter is installed " & _
"correctly and is detecting incoming")
SB.Append("\nspam. You can send yourself a test mail " & _
"containing the following string of")
SB.Append("\ncharacters (in upper case and with " & _
"no white spaces and line breaks):")
SB.Append("\nXJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-" & _
SB.Append("\nYou should send this test mail from an account " & _
"outside of your network.")
Dim Buffer() As Byte = _
Dim Socket As New System.Net.Sockets.Socket( _
Dim IPE As New System.Net.IPEndPoint( _
Dim R As Int32
Dim RecBuf(1024) As Byte
R = Socket.Receive(RecBuf)
Console.Write(System.Text.Encoding.ASCII.GetString(RecBuf, 0, R))
Loop Until R = 0
Result is the
SpamdClient class, which allows you to incorporate the spamc functionality into your software. Also is provided a console application (spamc.exe), allowing to communicate with the spamd server from command line.