spamassassin check score C# code
Asked Answered
T

2

8

Is there a way to check the score in an ASP.Net application? A class or something similar for .Net? How about other Spam Filters out there. --Edited I am looking for a way to check the spam score of the email messages in C#.

Tonus answered 18/2, 2011 at 7:11 Comment(5)
Check what score? You might be more detailed about what you are trying to do. tinyurl.com/so-hintsAiling
Ridvan, do you want to implememt a system which gets the emails from the server and checks what is spam and what is good? if so, what do you want to do with good and bad messages?Ornithischian
I am trying to implement a tool that would check the spam score of of an email message if From, Subject and Body is provided. Not when I receive the email, but before an email it is sent, or a web tool to show the spam score of an email. I have seen this codeproject.com/KB/recipes/BayesianCS.aspx but to me it is not really Spam AssasinTonus
There's MailingCheck which does just that as a quick desktop app. If you want programattic use I think you might have to run spamassassin as a service and use its service interface to query the score.Galatians
Postmark now host a JSON web service you can use tooGalatians
B
7

Here is my super simplified "just check the score" code for connecting to a running Spam Assassin email check from C# which I wrote for http://elasticemail.com. Just setup SA to run on a server and set the access permissions.

Then you can use this code to call it:

public class SimpleSpamAssassin
{
    public class RuleResult
    {
        public double Score = 0;
        public string Rule = "";
        public string Description = "";

        public RuleResult() { }
        public RuleResult(string line)
        {
            Score = double.Parse(line.Substring(0, line.IndexOf(" ")).Trim());
            line = line.Substring(line.IndexOf(" ") + 1);
            Rule = line.Substring(0, 23).Trim();
            Description = line.Substring(23).Trim();
        }
    }
    public static List<RuleResult> GetReport(string serverIP, string message) 
    {
        string command = "REPORT";

        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("{0} SPAMC/1.2\r\n", command);
        sb.AppendFormat("Content-Length: {0}\r\n\r\n", message.Length);
        sb.AppendFormat(message);

        byte[] messageBuffer = Encoding.ASCII.GetBytes(sb.ToString());

        using (Socket spamAssassinSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            spamAssassinSocket.Connect(serverIP, 783);
            spamAssassinSocket.Send(messageBuffer);
            spamAssassinSocket.Shutdown(SocketShutdown.Send);

            int received;
            string receivedMessage = string.Empty;
            do
            {
                byte[] receiveBuffer = new byte[1024];
                received = spamAssassinSocket.Receive(receiveBuffer);
                receivedMessage += Encoding.ASCII.GetString(receiveBuffer, 0, received);
            }
            while (received > 0);

            spamAssassinSocket.Shutdown(SocketShutdown.Both);

            return ParseResponse(receivedMessage);
        }

    }

    private static List<RuleResult> ParseResponse(string receivedMessage)
    {
        //merge line endings
        receivedMessage = receivedMessage.Replace("\r\n", "\n");
        receivedMessage = receivedMessage.Replace("\r", "\n");
        string[] lines = receivedMessage.Split('\n');

        List<RuleResult> results = new List<RuleResult>();
        bool inReport = false;
        foreach (string line in lines)
        {
            if (inReport)
            {
                try
                {
                    results.Add(new RuleResult(line.Trim()));
                }
                catch
                {
                    //past the end of the report
                }
            }

            if (line.StartsWith("---"))
                inReport = true;
        }

        return results;
    }

}

Usage is quite easy:

List<RuleResult> spamCheckResult = SimpleSpamAssassin.GetReport(IP OF SA Server, FULL Email including headers);

It will return the list of spam check rules you hit and the resulting score impact.

Bonnie answered 25/1, 2012 at 18:46 Comment(1)
Getting error on spamAssassinSocket.Receive(receiveBuffer) "System.Net.Sockets.SocketException: 'An existing connection was forcibly closed by the remote host'Ullund
F
2

I am not exactly sure if that's what you are searching for, but there is a C# wrapper that simplifies the communication with a SpamAssassin server on Code Project:

Hope that helps!

Frasco answered 21/2, 2011 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.