FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Always about Outlook and OLE
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Always about Outlook and OLE
Posted: Tue Jun 10, 2014 09:49 PM

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

Best Regards,



Marco Turco

SOFTWARE XP LLP
Posts: 16
Joined: Tue May 19, 2009 07:28 PM
Re: Always about Outlook and OLE
Posted: Wed Jun 11, 2014 07:01 PM

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

Pat Driscoll

Australia
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Re: Always about Outlook and OLE
Posted: Fri Jun 13, 2014 10:20 AM

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 ?

Best Regards,



Marco Turco

SOFTWARE XP LLP
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Always about Outlook and OLE
Posted: Sat Apr 01, 2017 10:11 AM

Marco,
have you found a solution to this problem.
Thanks in advance
Otto

Posts: 663
Joined: Mon Dec 05, 2005 11:22 PM
Re: Always about Outlook and OLE
Posted: Sat Apr 01, 2017 02:49 PM
It looks like SendUsingAccount requires an object.
This is untested
Code (fw): Select all Collapse
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
Posts: 274
Joined: Fri Apr 04, 2008 01:25 PM
Re: Always about Outlook and OLE
Posted: Mon Jul 17, 2017 01:49 PM
This works and is tested for getting the accounts:

Code (fw): Select all Collapse
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.
Best Regards,

Gilbert Kuhnert
CTO Software GmbH
http://www.ctosoftware.de
Posts: 274
Joined: Fri Apr 04, 2008 01:25 PM
Re: Always about Outlook and OLE
Posted: Mon Jul 17, 2017 03:22 PM
Working:

Code (fw): Select all Collapse
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
Best Regards,

Gilbert Kuhnert
CTO Software GmbH
http://www.ctosoftware.de

Continue the discussion