durs / node-activex

Node.JS Implementaion of ActiveXObject
MIT License
329 stars 62 forks source link

Can't Set the SendUsingAccount property when using the Outlook ActiveX #106

Closed somanuell closed 2 years ago

somanuell commented 2 years ago

Hi durs, I have a test case where cscript.exe does the work, but winax fails. When setting some property in some object, in node.exe the property is seen as null just after the affectation, but not in cscript.exe

Is there a syntax I should use to set the property's value?

Below, script for reference, the offending code is oMail.SendUsingAccount = oTheAccountToUse;

if ( typeof module !== 'undefined' ) module.exports = require('winax/activex');
function Log( aString ) {
    if ( typeof WScript !== 'undefined' ) {
        WScript.Echo( aString );
    } else {
        console.log( aString );
    }
    return;
}

Log( 'Starting...' );
var oApp = new ActiveXObject("Outlook.Application");
var oAccounts = oApp.Session.Accounts;
var oTheAccountToUse = null;
for (var accountIndex = 1; accountIndex <= oAccounts.Count; accountIndex++) {
    var oOneAccount = oAccounts.Item(accountIndex);
    if ( oOneAccount.SmtpAddress === 'some.adress@orange.fr' ) {
        oTheAccountToUse = oOneAccount;
        Log( 'Account Orange Found!' );
        break;
    } // endif
}
var oMail = oApp.CreateItem(0);
oMail.Recipients.Add('some.adress@google.com');
if ( typeof WScript !== 'undefined' ) {
    oMail.Subject = 'Test Mail with ActiveX CSCRIPT.EXE';
} else {
    oMail.Subject = 'Test Mail with ActiveX NODE.EXE';
}
if ( oTheAccountToUse !== null ) {
    Log( 'Setting Sender to: ' + oTheAccountToUse.SmtpAddress );
    oMail.SendUsingAccount = oTheAccountToUse;
    if ( oMail.SendUsingAccount === null ) {
        Log( 'oMail.SendUsingAccount seen as null !' );
    } else {
        Log( 'oMail.SendUsingAccount seen as NOT null !' );     
    }
}
oMail.Send();
Log('Sent!');
somanuell commented 2 years ago

When using the above script with node.exe (and with correct strings for the addresses...) the output is:

Starting... Account Orange Found! Setting Sender to: some.adress@orange.fr oMail.SendUsingAccount seen as null ! Sent!

somanuell commented 2 years ago

So, the mail is sent, but not FROM the correct address/account

somanuell commented 2 years ago

Hi durs, root cause FOUND!

If you look at the code of PutProperty in Microsoft's , you will find:

      if (pVar->vt == VT_UNKNOWN || pVar->vt == VT_DISPATCH ||
            (pVar->vt & VT_ARRAY) || (pVar->vt & VT_BYREF))
        {
            HRESULT hr = pDispatch->Invoke(dwDispID, IID_NULL,
                LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUTREF,

That "magic" is missing in your implementation. I added it, and my use case works!

Will submit a PR.