Page 1 of 1

Sending mail to Outlook fails under Windows 10

Posted: Sun Feb 23, 2020 9:58 am
by rdonnay
Has anyone had success running the \exp20\samples\outlook\outlook.prg program?

This always worked in the past.

Lots of errors now.

Re: Sending mail to Outlook fails under Windows 10

Posted: Sun Feb 23, 2020 10:23 am
by rdonnay
I determined that it will work only if Outlook is closed or Outlook is running as an Administrator.

This is new for Windows 10.

Re: Sending mail to Outlook fails under Windows 10

Posted: Wed Feb 26, 2020 4:02 pm
by Dian
We have ran into the same problem. Started happening after installing office security updates in Jan. Reverted back to Alaska 1.91 and had no problems, so it seems to be a Alaska 2.0 problem with using Mapi access to outlook.
We haven't been able to solve the problem yet.

Re: Sending mail to Outlook fails under Windows 10

Posted: Wed Feb 26, 2020 4:28 pm
by rdonnay
I am accessing it through the 'Outlook.Application' activex control.

This is what I had to do to make it work:

Code: Select all

FUNCTION SendMailToOutLook( cSubject, cText, acFile, acEmail )

LOCAL oOutlookApplication, oOutlookMailItem, i, lStatus, aWait, ;
      lReloadOutlook := .f., cOutlook

DEFAULT cSubject := '', ;
        cText := ''

IF Valtype(acFile) = 'C'
  acFile := { acFile }
ENDIF

IF Valtype(acEmail) = 'C'
  acEmail := { acEmail }
ELSEIF Empty(acEmail)
  acEmail := {}
ENDIF

aWait := Pl_Wait("Loading Outlook")
oOutlookApplication := CreateObject("Outlook.Application")
Pl_Wait(aWait)

IF Valtype(oOutlookApplication) # 'O'
  DCMSGBOX 'Cannot send mail to Outlook for one of the following reasons:', ;
           '', ;
           'Outlook is Running (not as an Administrator)', ;
           'Outlook is not installed on your computer', ;
           '', ;
           'Do you want the application to try and close Outlook and send an email?' ;
          FONT '10.Lucida Console' ;
          YESNO TO lStatus

  IF lStatus
    IF !KillTask('OUTLOOK.EXE')
      RETURN .f.
    ENDIF
    Sleep(200)
    lReloadOutlook := .t.
  ELSE
    RETURN .f.
  ENDIF
ENDIF

aWait := Pl_Wait("Loading Outlook")
oOutlookApplication := CreateObject("Outlook.Application")
Pl_Wait(aWait)

IF Valtype(oOutlookApplication) # 'O'
  DCMSGBOX 'Cannot send an email to Outlook. Sorry!'
  RETURN .f.
ENDIF

// Creating a new MailItem
oOutlookMailItem := oOutlookApplication:CreateItem(olMailItem)

 acFile := _getAttachments()

 // Setting the new MailItem subject and body
 IF Empty(cSubject) .AND. Len(acFile) > 0
   cSubject := "Documents From "+GetFlg('other18')
 ENDIF

 oOutlookMailItem:Subject := cSubject
 IF Empty(cText) .AND. Len(acFile) > 0
   cText := "Greetings,"+CR_LF+CR_LF+"Enclosed please find "+;
            Alltrim(Num_2Text(Len(acFile), .F. )) + " attached document" +;
            IIF( Len(acFile) <> 1, "s", "")+"."+CR_LF+CR_LF+;
            "Thank You"+CR_LF+CR_LF+UserFullName()
 ENDIF
 oOutlookMailItem:Body := cText

 // Setting the new MailItem recipients
 FOR i := 1 TO Len(acEmail)
   oOutlookMailItem:Recipients:Add(acEmail[i])
 NEXT

 // Adding the new MailItem a file attachment
 FOR i := 1 TO Len(acFile)
   IF !Empty(acFile[i])
     oOutlookMailItem:Attachments:Add(acFile[i])
   ENDIF
 NEXT

// Sending the new MailItem
oOutlookMailItem:Display()
// oOutlookMailItem:Send()

// Releasing Outlook
// oOutlookApplication:Quit()

oOutlookApplication:Destroy()

IF lReloadOutlook
  cOutlook := FindOutlook()
  IF !Empty(cOutlook)
    RunShell('',cOutlook)
  ENDIF
ENDIF

RETURN nil

* ------------

FUNCTION FindOutlook()

LOCAL aOutlook, i, cOutlook := ''

aOutlook := { ;
  'C:\Program Files (x86)\Microsoft Office\root\Office16\outlook.exe', ;
  'C:\Program Files (x86)\Microsoft Office\Office14\outlook.exe',;
  'C:\Program Files (x86)\Microsoft Office\Office12\outlook.exe',;
  'C:\Program Files (x86)\Microsoft Office\Office11\outlook.exe' }

FOR i := 1 TO Len(aOutlook)
  cOutlook := aOutlook[i]
  IF FExists(cOutlook)
    RETURN cOutlook
  ENDIF
NEXT

RETURN ''

* ------------

FUNCTION KillTask( cTaskToKill )

LOCAL aTasks, nFound

RunShell('/C TaskList.Exe /V /FO CSV > tasklist.csv')

aTasks := DC_Csv2Array('tasklist.csv')

IF Empty(aTasks)
  DCMSGBOX 'Could not create TASKLIST.CSV'
  RETURN .f.
ENDIF

IF !Empty(cTaskToKill)
  nFound := AScan(aTasks,{|a|Trim(Upper(a[1])) == Upper(cTaskToKill)})
  IF nFound > 0
    RETURN _KillTask(aTasks[nFound,2])
  ENDIF
ENDIF

RETURN .f.

* ---------

STATIC FUNCTION _KillTask( cPID )

RunShell('/PID ' + cPID,'C:\windows\system32\TaskKill.Exe',.t.,.f.)

RETURN .t.