spamassassin检查得分C#代码

时间:2011-02-18 07:11:54

标签: c# asp.net spam-prevention spamassassin

有没有办法检查ASP.Net应用程序中的分数? .Net的类或类似的东西?那里有其他垃圾邮件过滤器怎么样。 --Edited 我正在寻找一种方法来检查C#中的电子邮件的垃圾邮件分数。

2 个答案:

答案 0 :(得分:5)

这是我的超级简化"只需检查分数"用于连接到我为http://elasticemail.com编写的C#正在运行的Spam Assassin电子邮件检查的代码。只需将SA设置为在服务器上运行并设置访问权限。

然后您可以使用此代码来调用它:

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;
    }

}

使用非常简单:

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

它将返回您点击的垃圾邮件检查规则列表以及由此产生的分数影响。

答案 1 :(得分:1)

我不确定这是否是您要搜索的内容,但是有一个C#包装器可以简化与Code Project上的SpamAssassin服务器的通信:

希望有所帮助!