FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Thu Jun 08, 2017 05:58 PM

I labeled this OT since it is not about trying to fix an issue in FWH. I have the ability to use MAPI on my computer from within my application. I'm using Office 365 on Windows 10, and it has no problems.

Where I run into problems is on a few of my customers' machines. They may have older Outlook copies installed, and are running perhaps with Windows 7. In that case, MAPI does not open the Outlook email from within my program, even if Outlook is running on that machine. I have checked and Outlook is set as the default email client.

My request is for others to share what they have learned in their email experiences about how we need to have settings in Windows machines to be sure this works in the older OS's or older versions of Outlook.

Thanks for any, and all, ideas.

Tim Stone

Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Thu Jun 08, 2017 06:33 PM
Tim

I am not a big fan of sending e-mail from a client computer .. just too many variables, too many different operating systems, too many different clients e-mail ... Outlook Express, Live mail, Outlook ( from office ) or Thunderbird ..

I have found that using CDO is the most reliable solution that does not need to use any specific e-mail client on a machine .. to send messages to a gmail account I monitor. With Gmail you have to change your Gmail settings to use un-secure devices, otherwise CDO and g-mail work fairly well.

Here is the CDO code I have had the best results using ..

Rick Lipkin

Code (fw): Select all Collapse
//---------------------------------------------
Function _SendMail(cSmtp_Host,nPort,lSsl,;
                           cSmtp_UserId,cSmtp_Password,cFrom,cTo,aCC,cSubject,cMessage,oDlg)

Local oEmailCfg,oErr,lFailed,oEmailMsg,cAddress,i

*msginfo( "External" )

*msginfo( cSmtp_host )
*msginfo( nPort )
*msginfo( lssl )
*msginfo( cSmtp_UserId )
*msginfo( cSmtp_Password )
*msginfo( "Cfrom "+cFrom )
*msginfo( cTo )
*xbrowse(aCC)
*msginfo( cSubject )



SysReFresh()

// smtpauthenticate
// 0 cdoAnonymous Perform no authentication.
// 1 cdoBasic     Use the basic (clear text) authentication mechanism.
// 2 cdoNTLM      Use the NTLM authentication mechanism.

// sendusing
// Remote SMTP = 2, local = 1

If empty(aCC)
   cAddress := ""
Else
   For i = 1 to Len(aCC)
       If i = 1
          cAddress := aCC[i]
       Else
          cAddress := cAddress+","+aCC[i]
       Endif
    Next
Endif

lFailed := .f.
TRY
  oEmailCfg := CREATEOBJECT( "CDO.Configuration" )
  WITH OBJECT oEmailCfg:Fields
  :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value       := cSmtp_Host
  :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value   := nPort
  :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value        := 2
  :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value := 1
  :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value       := lSsl
  :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value     := cSmtp_Userid
  :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value     := cSmtp_Password
  :Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"):Value := 15
  :Update()
  END WITH

CATCH oError
   MsgInfo( "Error in Configuration" )
END

oError := NIL
TRY
   oEmailMsg  := CREATEOBJECT ( "CDO.Message" )
   WITH OBJECT oEmailMsg
        :Configuration = oEmailCfg
        :From     := cFrom //"me@bogus.com"
        :To       := cTo
        :CC       := cAddress

    //  :CC       := ""
        :BCC      := ""
        :Subject  := cSubject
     // :MDNRequested = .T.       // Solicitud de reconocimiento, o acuse de recibo
        :TextBody := cMessage

        * for each cFile in ::aFiles
                *    :AddAttachment( cfile )
        * next
        :Fields:update()
        :Send()
     // ? "[ "+Time()+" ] Enviado correo :"+ ::email
   END
CATCH oError
      MsgINfo("Error in sending e-mail:"+  oError:Description )
      lFailed := .t.
END

oEmailCfg := NIL
oEmailMsg := NIL

oDlg:End()
SysRefresh()

Return(lFailed)
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Thu Jun 08, 2017 07:11 PM
Rick,

I actually have the setup so my client can use SMTP, CDO, or MAPI. My SMTP and CDO routines work fine, but some want everything to go through MAPI ( Outlook ) so they can see Sent emails and all responses in one place.

SO ... that is why the question was specific to MAPI. It works great here and on many of my clients, but one person is having "issues" ... and perhaps others.

There is one other BIG advantage to MAPI. While SMTP and CDO are set system wide, MAPI allows them to have emails go out from separate accounts ... ie. sales, service, info, etc.

Here is my MAPI code. As I said, it works great on many setups:

Code (fw): Select all Collapse
FUNCTION SendMAPIMail2( MailTo, cSndFile, cText, cSubj )
    // Send MAPI mail originates email to the MAPI client
    //  Updated:    8/2/2015 2:16:52 PM
    PRIVATE oMail
      DEFINE MAIL oMail ;
         SUBJECT cSubj ;
         TEXT cText ;
         FILES cSndFile, cSndFile ;
         FROM USER ;
         TO MailTo
      ACTIVATE MAIL oMail
RETURN( .t. )


Tim
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Fri Jun 09, 2017 04:34 PM
Tim,

In that case, MAPI does not open the Outlook email from within my program, even if Outlook is running on that machine.


Any error or other messages? Or just nothing happens?

Have you tried a short test program?

Some versions of Outlook will not allow mail via MAPI. I know that some versions will popup a message asking if you want to allow third party program to send via Outlook. Possibly that is up in the background? There are programs that will override this behavior.

I would change "PRIVATE oMail" to "LOCAL oMail" in your code. I never use PRIVATES or PUBLICS--too many issues. It forces you to encapsulate so you eliminate many hard to find bugs.

Regards,
James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Fri Jun 09, 2017 04:38 PM

James,

It's always hard to test something when my system works perfectly, and the problem isolates to a single client ...

The problem on their machine is that nothing happens ... no error message ... nothing ...

Part of the problem is that they claim it used to work, and doesn't work with a more recent version of the program ... but the code has been the same all along.

Tim

Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Sun Jun 11, 2017 08:11 PM

Tim,

I see another issue with your code. You need to do:

oMail:end()

At the end of your routine. The End() method calls MapiLogoff(), so this may be significant. It could create a memory issue, or a CDO issue.

METHOD End() INLINE MAPILogOff()

I suggest fixing that and declaring oMail as local. Then testing it again on the offending machine. If that doesn't solve it, then it would indicate that something else on that machine has changed, like an update.

Encapsulate, encapsulate, encapsulate...

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Mon Jun 12, 2017 09:56 PM
James Bott wrote:Tim,

I see another issue with your code. You need to do:

oMail:end()

At the end of your routine. The End() method calls MapiLogoff(), so this may be significant. It could create a memory issue, or a CDO issue.

METHOD End() INLINE MAPILogOff()


No, it is not required. A MAPI session is automatically created and released if you don't use MAPILogon().

EMG
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: OT: MAPI -- Why it sometimes work, and sometimes doesn't ?
Posted: Mon Jun 12, 2017 10:53 PM

Enrico,

Good to know. I just looked at the source (TMail.prg) and I see the the mapiLogon() is commented out now.

Thanks for pointing it out.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10

Continue the discussion