7

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#.

Ridvan
  • 71
  • 1
  • 4
  • Check what score? You might be more detailed about what you are trying to do. http://tinyurl.com/so-hints – Albin Sunnanbo Feb 18 '11 at 07:27
  • 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? – Davide Piras Feb 18 '11 at 07:46
  • 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 http://www.codeproject.com/KB/recipes/BayesianCS.aspx but to me it is not really Spam Assasin – Ridvan Feb 18 '11 at 08:12
  • There's [MailingCheck](http://www.mailingcheck.com/download-spam-checker/) 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. – Rup Feb 18 '11 at 10:22
  • Postmark now [host a JSON web service](http://spamcheck.postmarkapp.com/) you can use too – Rup Jan 26 '12 at 09:45

2 Answers2

5

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.

Joshua
  • 3,195
  • 3
  • 25
  • 19
  • Getting error on spamAssassinSocket.Receive(receiveBuffer) "System.Net.Sockets.SocketException: 'An existing connection was forcibly closed by the remote host' – Naveen Jan 18 '19 at 11:28
1

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!

Martin Buberl
  • 42,048
  • 25
  • 96
  • 139