Not able to read return value from spamassassin when launched from C# console
I have a windows server 2008 with a mail server installed. For every emails that comes in, a C# application is launched that pipes the message through a set of optional filters and then decides whether or not to let it throught.
I have alrealdy a couple of "homemade" filters implemented but I would like to add one to take advantage of SpamAssassin's power. Since SA is UNIX based, I'm working with its window port (SAWin32). When I launch SpammAssassin directly from command line, email gets analysed correctly:
spamassassin.exe -L -e < C:\SPAM.MAI
(-L enables only local test, -e exits with a non zero value if the tested message was spam)
But when I try to launch it from my C# application, it doesn't seem to work since the return value is always equal to 2 (even for ham messages):
Process proc = new Process();
proc.StartInfo.FileName = @"C:\spamassassin.exe";
proc.StartInfo.Arguments = " -L -e <开发者_StackOverflow; C:\SPAM.MAI";
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
Console.WriteLine("Exit code: " + exitCode);
proc.Close();
I would like to know if there is something wrong with my code and/or if anyone has experience launching SAWin32 from a C# console application?
Thanks a lot!
After digging a while and reading Ian P's comment, I finally got it working:
Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = @" /C C:\spamassassin.exe -e -L < C:\SPAM_TEST.MAI";
p.StartInfo.FileName = @"C:\WINDOWS\System32\cmd.exe";
p.OutputDataReceived += (sender, arguments) => Console.WriteLine("Received output: {0}", arguments.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
Console.WriteLine("Exit code: " + p.ExitCode);
p.Close();
The /C parameter tells CMD to carry out the command and then to terminate.
Hope this helps someone!
精彩评论