2011-02-18 50 views
5

有沒有辦法在ASP.Net應用程序中檢查分數?一個類或類似的.Net?那麼其他垃圾郵件過濾器如何呢? - 編輯 我正在尋找一種方法來檢查C#中電子郵件的垃圾郵件分數。spamassassin檢查分數C#代碼

+0

查看得分?你可能會對你想要做的更詳細。 http://tinyurl.com/so-hints – 2011-02-18 07:27:08

回答

4

這是我的超級簡化的「只檢查分數」代碼,用於連接到我爲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); 

它會返回你打的垃圾郵件檢查規則列表中,所得分數的影響。