8

ASP.Net アプリケーションでスコアを確認する方法はありますか? .Net のクラスまたは同様のものですか? 他のスパム フィルタについてはどうでしょうか。--編集済み C# で電子メール メッセージのスパム スコアをチェックする方法を探しています。

4

2 に答える 2

6

これは、 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);

ヒットしたスパム チェック ルールのリストと、その結果のスコアへの影響が返されます。

于 2012-01-25T18:46:28.190 に答える
2

それがあなたが探しているものかどうかは正確にはわかりませんが、Code Project の SpamAssassin サーバーとの通信を簡素化する C# ラッパーがあります。

それが役立つことを願っています!

于 2011-02-21T18:11:21.073 に答える