spamassassin check score C# code

巧了我就是萌 提交于 2019-12-21 04:31:16

问题


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


回答1:


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.




回答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:

  • A C# Wrapper for the SpamAssassin Protocol

Hope that helps!



来源:https://stackoverflow.com/questions/5038574/spamassassin-check-score-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!