Page 1 of 1

Always about Outlook and OLE

Posted: Tue Jun 10, 2014 9:49 pm
by Marco Turco
Hi,
I have several emails accounts on Outlook.
For example: nick@softwarexp.co.uk (default), alan@softwarexp.co.uk, mary@softwarexp.co.uk.

How can I send an email via ole to Outlook using a sender different from the default account ? How for example can i send an email using the mary@softwarexp.co.uk account ?
Using the following code, the email is sent always using the default account (nick@softwarexp.co.uk)

oOutLook := TOleAuto():New("Outlook.Application")
oMailItem := oOutLook:CreateItem( 0 )
oRecip := oMailItem:Recipients

Thank you in advance

Re: Always about Outlook and OLE

Posted: Wed Jun 11, 2014 7:01 pm
by patdriscoll
Hi Marco,

Have you tried something like:

cAccount := "xyz@xyz.com"
oMailItem:SendUsingAccount := cAccount

where xhz@xyz.com is one of the accounts in your Outlook

Cheers,

Pat

Re: Always about Outlook and OLE

Posted: Fri Jun 13, 2014 10:20 am
by Marco Turco
Yes, tried with this code but the email is Always sent using the default Outlook account.

oOutLook := TOleAuto():New("Outlook.Application")
oMailItem := oOutLook:CreateItem( 0 )
oMailItem:SendUsingAccount := "xxxx@yyy.zzz"
oRecip := oMailItem:Recipients
..
..

Any ideas ?

Re: Always about Outlook and OLE

Posted: Sat Apr 01, 2017 10:11 am
by Otto
Marco,
have you found a solution to this problem.
Thanks in advance
Otto

Re: Always about Outlook and OLE

Posted: Sat Apr 01, 2017 2:49 pm
by Gale FORd
It looks like SendUsingAccount requires an object.
This is untested

Code: Select all | Expand


oOutLook := TOleAuto():New("Outlook.Application")
oAccount := oOutlook:Session:Accounts:Items( 1 )  // Default Account
For nCounter := 1 to len( oOutlook:Session:Accounts )
  if oOutlook:Session:Accounts:Items( nCounter ):SmtpAddress = "xxxx@yyy.zzz"
    oAccount :=  oOutlook:Session:Accounts:Items( nCounter )
    exit
  endif
next
oMailItem := oOutLook:CreateItem( 0 )
oMailItem:SendUsingAccount := oAccount
 

Re: Always about Outlook and OLE

Posted: Mon Jul 17, 2017 1:49 pm
by gkuhnert
This works and is tested for getting the accounts:

Code: Select all | Expand

static function GetOutlookAccounts()
local aData := {}
local oAccounts, oAccount
local nI
    oOutlook := GetOutlookObject()
    IF oOutlook == nil
        return nil
    ENDIF
    oAccounts := oOutlook:Session:Accounts()
    FOR EACH oAccount in oAccounts
        aAdd(aData, {oAccount:SmtpAddress, oAccount:DisplayName, oAccount:UserName, oAccount:AccountType})
    NEXT
    xbrowse(aData)
return nil

static function GetOutlookObject()
    TRY
        oOutlook := GetActiveObject ( "Outlook.Application" )
    CATCH
        TRY
        oOutlook := CREATEOBJECT( "Outlook.Application" )
        CATCH
                MsgStop("Cannot start outlook.")
                return nil
            END
        END
return oOutlook

 


You just have to pick out the righ oAccount and use it, I assume.

Re: Always about Outlook and OLE

Posted: Mon Jul 17, 2017 3:22 pm
by gkuhnert
Working:

Code: Select all | Expand

static oOutlook

static function SendTestMail(cAddressTo, cAddressFrom)
local oMail
    GetOutlookObject()
    IF oOutlook == nil
        return nil
    ENDIF
    oMail := CreateOutlookMail(cAddressTo, cAddressFrom)
    oMail:Send()
return .t.

static function CreateOutlookMail(cAddressTo, cAddressFrom)
local oItem, oAccount
    oItem := oOutlook:Application:CreateItem(0)
    oItem:Subject := "Mail to myself"
    oItem:Body := "Body of my testmail"
    IF !empty(cAddress)
        oAccount := GetAccountByAddress(cAddressFrom)
        IF oAccount != nil
            oItem:SendUsingAccount := oAccount
        ENDIF
    ENDIF
    oItem:Recipients():Add(cAddressTo)
return oItem

static function GetAccountByAddress(cAddress)
local oTemp, oAccounts, oAccount
    oAccounts := oOutlook:Session:Accounts()
    FOR EACH oTemp in oAccounts
        IF upper(alltrim(oTemp:SmtpAddress)) == upper(alltrim(cAddress))
            oAccount := oTemp
        ENDIF
    NEXT
return oAccount

static function GetOutlookObject()
    TRY
        oOutlook := GetActiveObject ( "Outlook.Application" )
    CATCH
        TRY
        oOutlook := CREATEOBJECT( "Outlook.Application" )
        CATCH
                MsgStop("Cannot start outlook.")
                return nil
            END
        END
return osOutlook