function OnAcceptMessage(oClient, oMessage)
{
    var fromAddr = oMessage.FromAddress;
    var from = oMessage.From;
    // var toAddr = oMessage.Recipients.Item(0).Address; // dit zal catchall@.... zijn
    var toAddr = oMessage.Recipients.Item(0).OriginalAddress; // heeft geen last van de catchall

    EventLog.write("From: " + from);
    if (fromAddr != from)
        EventLog.write("FromAddr: " + fromAddr);
    EventLog.write("To: " + toAddr);

    var domain = toAddr.split("@")[1]; // Bijvoorbeeld xxxx.facilitor.nl. Daar doen we de API-call ook naar toe

    var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.6.0");
    xmlDoc.appendChild(xmlDoc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""))

    rootElement = xmlDoc.createElement("facilitor");
        elementMail = xmlDoc.createElement("mail");
            // velden uit https://www.hmailserver.com/documentation/v4.4/?page=com_message
            addtext(xmlDoc, elementMail, "subject",  oMessage.Subject);
            addtext(xmlDoc, elementMail, "from",     from);
            addtext(xmlDoc, elementMail, "fromaddr", fromAddr);
            addtext(xmlDoc, elementMail, "to",       toAddr);
            addtext(xmlDoc, elementMail, "body",     oMessage.Body);
            addtext(xmlDoc, elementMail, "htmlbody", oMessage.HTMLBody);
            // Wat extra velden waar api_hmail (nog) niets mee doet maar mogelijk ooit wel
            addtext(xmlDoc, elementMail, "CC",       oMessage.CC);
            addtext(xmlDoc, elementMail, "Charset",  oMessage.Charset);
            addtext(xmlDoc, elementMail, "Date",     oMessage.Date);
            addtext(xmlDoc, elementMail, "Size",     oMessage.Size); // Kilobyte trouwens

            addtext(xmlDoc, elementMail, "now",      new Date()); // For debugging
            elementAttachments = xmlDoc.createElement("attachments");
            for (i=0; i < oMessage.Attachments.Count; i++)
            {
                elementAttachment = xmlDoc.createElement("attachment");
                    addtext(xmlDoc, elementAttachment, "filename", oMessage.Attachments.Item(i).fileName);
                    addtext(xmlDoc, elementAttachment, "size", String(oMessage.Attachments.Item(i).Size));
                    // Of gewoon echt de attachments invoegen? api_mldsoap.asp kan ze al opvangen
                elementAttachments.appendChild(elementAttachment);
            }
            elementMail.appendChild(elementAttachments);

        rootElement.appendChild(elementMail);
    xmlDoc.appendChild(rootElement);

    var fso = new ActiveXObject("Scripting.FileSystemObject")

    // Zoek de ini-file in de hmail script folder
    // Soms(!)  Ensure that hMailServer.Application.Authenticate() is called with proper login credentials.
    //var hs = new ActiveXObject("hMailServer.Scripting")
    //EventLog.write("scd:" + hs.Directory); // geeft "An error occured processing the request"
    //var inifile = hs.CurrentScriptFile.replace(/\.js$/, ".config.js");
    var inifile = "c:\\Program Files (x86)\\hMailServer\\Events\\EventHandlers.config.js"
    if (!fso.FileExists(inifile))
        var inifile = "c:\\Program Files\\hMailServer\\Events\\EventHandlers.config.js"

    var f = fso.OpenTextFile(inifile, 1); // ForReading
    var configtxt = f.ReadAll();
    var config = eval('(' + configtxt + ')')
    f.Close();

    var cfgdata = config[domain];
    if (!cfgdata)
    {   // Gebeurt op LABS bij uitgaande e-mail van Logcenter
	    EventLog.write("Mail domain '" + domain + "' not configured. Outgoing?");
//        EventLog.write("ini:" + inifile);
//        Result.Message = "mail domain not configured";
//        Result.Value = 2; // reject
        return;
    }

    // API HMAIL moet wijzen naar appl/api/api_hmail.asp
    var xmlurl = cfgdata.url;

    var http_request = new ActiveXObject("WinHTTP.WinHTTPRequest.5.1");
    http_request.open("POST", xmlurl, false); // Synchroon
    http_request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    http_request.send(xmlDoc.xml);
    if (http_request.status != 200)
    {
        EventLog.write("http_request failed");
        EventLog.write(http_request.status);
        EventLog.write(http_request.ResponseText);
        Result.Message = "mail processing error";
        Result.Value = 2; // reject
        return;
    }
    var data = eval('(' + http_request.ResponseText + ')');
    if (data.maillog)
        EventLog.write(data.maillog);

    if (!data.success)
    {
        EventLog.write(data.message);
        Result.Message = data.message;
        Result.Value = 2; // reject
        return;
    }
    // api_hmail.asp heeft bepaald wat veilige extensies zijn en waar ze naar toe moeten
    for (i = 0; i < data.attachments.length; i++)
    {
        var sequence = data.attachments[i].sequence; // de onveilige zijn er uit gestript, daarom niet per se i==sequence
        var safefilename = data.attachments[i].safefilename;
        var oAttach = oMessage.Attachments.Item(sequence);
        EventLog.write(oAttach.fileName + ' bijlage (' + oAttach.Size + ' bytes) naar ' + safefilename);
        oAttach.SaveAs(safefilename);
    }
    EventLog.write("----------");
}

function addtext(xmlDoc, elementParent, tag, tekst)
{
    var elementTag = xmlDoc.createElement(tag);
        var elementText = xmlDoc.createTextNode(tekst);
        elementTag.appendChild(elementText);
    elementParent.appendChild(elementTag);
}

