gpt4 book ai didi

c# - spamassassin 检查分数 C# 代码

转载 作者:太空狗 更新时间:2023-10-29 20:25:37 25 4
gpt4 key购买 nike

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

最佳答案

这是我为 http://elasticemail.com 编写的用于从 C# 连接到正在运行的 Spam Assassin 电子邮件检查的 super 简化的“只需检查分数”代码.只需设置 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);

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

关于c# - spamassassin 检查分数 C# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5038574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com