Du kannst einen Transport Agent in .NET (C#) schreiben, um die E-Mail zu überwachen und bei Bedarf ein Python Script über externe Prozesse auszuführen.
using System;
using System.IO;
using Microsoft.Exchange.Data.Transport;
using Microsoft.Exchange.Data.Transport.Smtp;
using Microsoft.Exchange.Data.Transport.Email;
using Microsoft.Exchange.Data.Mime;
using System.Diagnostics;
namespace MyEmailTransportAgent
{
public class MyTransportAgent : SmtpReceiveAgent
{
public MyTransportAgent()
{
this.OnEndOfData += OnEndOfDataHandler;
}
private void OnEndOfDataHandler(ReceiveMessageEventSource source, EndOfDataEventArgs e)
{
try
{
using (var messageStream = new MemoryStream())
{
e.DataStream.CopyTo(messageStream);
messageStream.Position = 0;
var receivedMessage = new EmailMessage(new MimeDocument(messageStream));
// Process the received email details
RunPythonScript("c:\\meinscript.py", receivedMessage);
}
}
catch (Exception ex)
{
// Handle exceptions here
EventLog.WriteEntry("MyTransportAgent", $"Error: {ex.Message}", EventLogEntryType.Error);
}
}
private void RunPythonScript(string scriptPath, EmailMessage message)
{
ProcessStartInfo psi = new ProcessStartInfo
{
string fromAddress = message.From.SmtpAddress;
string subject = message.Subject;
FileName = "python", // Name des Python-Interpreters
Arguments = $"{scriptPath} {subject}", // Pfad zum Skript und etwaige Argumente
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = new Process())
{
process.StartInfo = psi;
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// Verarbeiten Sie das Ergebnis..
}
}
}
}
Danach registrierst du den Agent im Exchange Server.
New-TransportAgent -Name "MyTransportAgent" -TransportAgentFactory "MyEmailTransportAgent.MyTransportAgentFactory" -AssemblyPath "C:\Path\To\Your\Agent.dll"
und aktivierst ihn.
Enable-TransportAgent -Identity "MyTransportAgent"