8

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

4

2 回答 2

6

这是我为http://elasticemail.com编写的从 C# 连接到正在运行的垃圾邮件刺客电子邮件检查的超级简化的“只检查分数”代码。只需将 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);

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

于 2012-01-25T18:46:28.190 回答
2

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

希望有帮助!

于 2011-02-21T18:11:21.073 回答