oMail:HTMLBody problem

Post Reply
Marc Vanzegbroeck
Posts: 1102
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium
Contact:

oMail:HTMLBody problem

Post by Marc Vanzegbroeck »

Hi,

I have a very strange problem with oMail:HTMLBody
I allways used it without any problem.
I have a customer that runs my program on a network.
On 2 PC it's working fine, but on one PC the body is empty.
All 3 PC's run the same EXE-file from the same SQL-database. All 3 PC's have the same outlook 365.
The email-adres is filled correctly, also the subject and the attachements. Only the body is empty.
The vHTMLText is also comming from the database, and is 3 times the same. :shock:
oMail:Subject = vTitel
oMail:Attachments:Add( vBijlage )
oMail:HTMLBody = vHTMLText
oMail:Display = .T.
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: oMail:HTMLBody problem

Post by karinha »

Code: Select all

FUNCTION CDOSendMail( aTabMail )

   LOCAL oEmailCfg, oEmailMsg

   /* Tabmail
   01 = MAILSERVER  // A correct mail server address
   02 = MAILFROM  // A valid originator of the message
   03 = MAILTO  // Who it is being sent to
   04 = SUBJECT  // The subect
   05 = BODY  // The body in text format
   06 = ATTACHMENT  // Attachment(s)
   07 = BCC  // Usually NIL  NOT USED
   08 = LAUTHORIZATION  // T or F logical for authentication
   09 = USERID pour MAILSERVER  // The mail server Username
   10 = PW     pour MAILSERVER  // The mail server password
   11 = Mail port   defaut = 25  // The port, default is 25, set to 465 for SSL
   12 = Mail HTML page  // An HTML page, usually not used
   13 = ssl authentification  // T or F logical for SSL
   14 = Mail sender  // Mail sender name  NOT USED
   15 = Organisation sender  // Organization sender  NOT USED
   16 = Host  // Host if needed  NOT USED
   */

   TRY
      oEmailCfg := CREATEOBJECT( "CDO.Configuration" )
      WITH OBJECT  oEmailCfg:Fields
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value :=   TRIM( aTabMail[01] ) //"mail.xxxxxxxx.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value :=  aTabMail[11] // 25
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value := 2   // Remote SMTP = 2, local = 1
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value :=  aTabMail[08] // .T.
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value :=  aTabMail[13]
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value := TRIM( aTabMail[09] ) //  "xxanser@xxxxxxxx.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value := TRIM( aTabMail[10] ) // "xxxxxx"
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout" ):Value := 30
         :Update()
      END WITH
   CATCH oError
      MsgInfo( "Could not create message configuration" + ";"  + ;
         "Error: " + TRANSFORM( oError:GenCode, NIL ) + ";" + ;
         "SubC: " + TRANSFORM( oError:SubCode, NIL ) + ";" + ;
         "OSCode: " + TRANSFORM( oError:OsCode, NIL ) + ";" + ;
         "SubSystem: " + TRANSFORM( oError:SubSystem, NIL ) + ";" + ;
         "Message: " + oError:Description )
      RETURN .F.
   END
   oError := NIL

   TRY
      oEmailMsg := CREATEOBJECT ( "CDO.Message" )
      WITH OBJECT oEmailMsg
         :Configuration =  oEmailCfg
         :From = aTabMail[02] //chr(34)+" Anser K.K. "+chr(34)+ "<anser@xxxxxxxx.com>" // This will be displayed in the From (The email id does not appear)
         :To = TRIM( aTabMail[03] ) // "xxanserkk@xxxxx.com"    // <-----   Place the TO email address
         :Subject = aTabMail[04] // "This is a Tst message"
         :MDNRequested = .T.
         :TextBody =  TRIM( aTabMail[05] )
         IF LEN( aTabMail[06] ) > 0
            FOR nEle := 1 TO Len( aTabMail[06] )
               :AddAttachment( ALLTRIM( aTabMail[06][nEle] ) ) // := AllTrim( aAttach[ nEle ] )
            NEXT
         ENDIF
      END WITH
      oEmailMsg:Send( )
   CATCH oError
        
      MsgInfo( "Could not send message" + ";"  + CRLF + ;
         "Error: " + TRANSFORM( oError:GenCode, NIL ) + ";" + CRLF + ;
         "SubC: " + TRANSFORM( oError:SubCode, NIL ) + ";" + CRLF + ;
         "OSCode: " + TRANSFORM( oError:OsCode, NIL ) + ";" + CRLF + ;
         "SubSystem: " + TRANSFORM( oError:SubSystem, NIL ) + ";" + CRLF + ;
         "Message: " + oError:Description )
      RETURN .F.
    
   END

   RETURN( .T. )
 
//-------------------------------------------------------------------------------------------------------------------------------*

FUNCTION SendMail( oDlg, cSender, cPass, cDisplay, cReply, lSave, cTo, cCC, cSubject, cMsg, lReceipt, cAttach, lMsgInfo )

//-------------------------------------------------------------------------------------------------------------------------------*
   LOCAL oEmailCfg, oEmailMsg, oError, cHtml, cLine, n
   LOCAL nSuccess

   nSuccess := 1

DEFAULT lReceipt := .T. , lMsgInfo := .F. , lSave := .T. , cAttach := '', cSubject := '', cDisplay := MEMVAR->coname, cMsg := '', cCC := '', ;
      cReply := cSender, cGstNo := ''

   cMsg := alltrim( cMsg )

   CursorWait()

   cHtml := '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'
   cHtml += '<HTML><HEAD>'
   cHtml += '<META content="text/html; charset=windows-874" http-equiv=Content-Type>'
   cHtml += '<META name=GENERATOR content="MSHTML 8.00.6001.18783">'
   cHtml += '<STYLE></STYLE>'
   cHtml += '</HEAD>'
   cHtml += '<BODY bgColor=#ffffff>'
   cHtml += '<DIV>'

// cHtml += '<DIV><FONT size=2 color=blue face=Arial>Hello How are you ?</FONT></DIV></BODY></HTML>'

   cHtml += cMsg
   cHtml += '</DIV></BODY></HTML>'

   TRY
      oEmailCfg := CREATEOBJECT( "CDO.Configuration" )
      WITH OBJECT oEmailCfg:Fields
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value                 := "smtp.gmail.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value         := 465
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value              := 2   // Remote SMTP = 2, local = 1
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value       := .T.
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value                 := .T.
         :Item( "http://schemas.microsoft.com/cdo/configuration/savesentitems" ):Value          := lSave
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value           := cSender  //  "email@gmail.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value           := cPass // Password
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout" ):Value := 60
         :Update()
      END WITH

   CATCH oError

      IF lMsgInfo
         MsgAlert( "Could not send message" + CRLF +  ;
            "Error: " + TRANSFORM( oError:GenCode, NIL ) + CRLF +  ;
            "SubC: " + TRANSFORM( oError:SubCode, NIL ) + CRLF +  ;
            "OSCode: " + TRANSFORM( oError:OsCode, NIL ) + CRLF +  ;
            "SubSystem: " + TRANSFORM( oError:SubSystem, NIL ) + CRLF +  ;
            "Message: " + oError:Description )
      ELSE
         nSuccess := 0
      end
   END
   oError := NIL

   TRY
      oEmailMsg := CREATEOBJECT ( "CDO.Message" )
      WITH OBJECT oEmailMsg
         :Configuration  := oEmailCfg
         :From               := chr( 34 ) + cDisplay + " " + chr( 34 ) + "<" + cReply + ">" // cSender  // This will be displayed in the From (The email id does not appear)
         :To                 := cTo   // "dutch@easyfo.com"    // <-----   Place your email address
         :Subject            := cSubject  //   "Email Test Message from GMail"
         :ReplyTo            := cReply
         :MDNRequested   := .F.
         IF !empty( cAttach )
            :AddAttachment( cAttach )
         end
         :HTMLBody = cHtml
      END WITH
      oEmailMsg:Send()
   CATCH oError
      IF lMsgInfo
         MsgAlert( "Could not send message" + ";"  + CRLF + ;
            "Error: " + TRANSFORM( oError:GenCode, NIL ) + ";" + CRLF + ;
            "SubC: "  + TRANSFORM( oError:SubCode, NIL ) + ";" + CRLF + ;
            "OSCode: " + TRANSFORM( oError:OsCode, NIL ) + ";" + CRLF + ;
            "SubSystem: " + TRANSFORM( oError:SubSystem, NIL ) + ";" + CRLF + ;
            "Message: " + oError:Description )
      end
   END

   CursorArrow()

   RETURN nSuccess
 
João Santos - São Paulo - Brasil
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: oMail:HTMLBody problem

Post by karinha »

Code: Select all

// Enviando email com HARBOUR 3.2 by Rubens MDV Informatica e papelaria
// Modificado Por: Joao Santos em: 12/12/2017 - Many Thanks Rubens.

#include "Fivewin.ch"
#include "Mail.ch"
#include "error.ch"
#include "fileio.ch"
#include "xHb.ch"
#include "hbcompat.ch"
#include "hbssl.ch"

FUNCTION MAIN()

   LOCAL aArquivo  := ""
   LOCAL cAssunto  := "PROGRAMA DO RUBENS-> NO MEU HARBOUR FUNCIONA."
   LOCAL cMensagem := "MENSAGEM DO EMAIL DO RUBENS"
   LOCAL cImagem   := ""
   LOCAL lInformaEnvio := .T.

   Envia_Email( aArquivo, cAssunto, cMensagem, lInformaEnvio )

   RETURN NIL

FUNCTION Envia_Email( aArquivo, cAssunto, cMensagem, cImagem, lInformaEnvio )

   LOCAL lOk := .T.
   LOCAL AFILES, CSUBJECT, AQUEM, CMSG, CSERVERIP, CFROM, CUSER, CPASS, ;
         VPORTSMTP, ACC, ABCC, LCONF, LSSL

   hb_Default( @aArquivo, {} )
   hb_Default( @cAssunto, "XML e PDF de Nota Fiscal" )
   hb_Default( @cMensagem, "Envio de Email" )
   hb_Default( @cImagem, "" )
   hb_Default( @lInformaEnvio, .T. )

//hb_Default(@cFrom,"MDV Informatica e papelaria ")

//hb_Default(@aQuem,"Rubens - MDV Informatica - Hotmail ")
   
   aFiles    := aArquivo // pode ser uma matriz com vários endereços
   cSubject := cAssunto
   aQuem     := "kapiabafwh@gmail.com"  // cFrom
   cMsg     := cMENSAGEM
   cServerIp := "smtp.mail.gmail.com"   // servidor smtp
   cFrom     := "kapiabafwh@gmail.com"
   cUser     := "kapiabafwh@gmail.com"  // cEMAIL
   cPass     := "mypassword"            // cSENHAEMAIL
   vPORTSMTP := 587
   aCC      := ""
   aBCC     := ""
   lConf     := .F.
   lSSL     := .F.  // OR .T.
   
   lOk := Config_Mail( aFiles, ;
      cSubject, ;
      aQuem, ;
      cMsg, ;
      cServerIp, ;
      cFrom, ;
      cUser, ;
      cPass, ;
      vPORTSMTP, ;
      aCC, ;
      aBCC, ;
      lConf, ;
      lSSL, ;
      cImagem, ;
      lInformaEnvio )

   RETURN lOk

//*******************************************************************************

FUNCTION Config_Mail( aFiles, cSubject, aQuem, cMsg, cServerIp, cFrom, cUser, cPass, vPORTSMTP, aCC, aBCC, lConf, lSSL, cImagem, lInformaEnvio )

   LOCAL lRet
   LOCAL oCfg, oErroMail
   LOCAL lAut := .T.
 
   hb_Default( @cImagem, "" )
   hb_Default( @lInformaEnvio, .T. )

   TRY
      oCfg := win_OleCreateObject( "CDO.Configuration" )
      WITH OBJECT oCfg:Fields
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value       := cServerIp
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value   := vPORTSMTP
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value        := 2
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value := lAut
         :Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value       := lSSL
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value     := AllTrim( cUser )
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value     := AllTrim( cPass )
         :Update()
      END WITH
      lRet := .T.
   CATCH oErroMail

      IF lInformaEnvio
         HB_ALERT( WIN_OEMTOANSI( "NÆo foi poss¡vel enviar o e-mail!"           + hb_EOL() + ;
            "Error: "  + Transform( oErroMail:GenCode, nil ) + ";" + hb_EOL() + ;
            "SubC: "   + Transform( oErroMail:SubCode, nil ) + ";" + hb_EOL() + ;
            "OSCode: "  + Transform( oErroMail:OsCode,  nil ) + ";" + hb_EOL() + ;
            "SubSystem: " + Transform( oErroMail:SubSystem, nil ) + ";" + hb_EOL() + ;
            "Mensagem: " + oErroMail:Description ), "Aten‡Æo", 150, 10000, 2, .T. )
      ENDIF

      lRet := .F.

   END

//--> FIM DAS CONFIGURAÇOES.
   IF lRet
      lRet := Envia_Mail( oCfg, ;
         cFrom, ;
         aQuem, ;
         aFiles, ;
         cSubject, ;
         cMsg, ;
         aCC, ;
         aBCC, ;
         lConf, ;
         lAut, ;
         lSSL, ;
         cServerIp, ;
         cImagem, ;
         lInformaEnvio )
   ENDIF

   RETURN lRet

//*******************************************************************************

FUNCTION Envia_Mail( oCfg, cFrom, cDest, aFiles, cSubject, cMsg, aCC, aBCC, vEmaiL_Conf, lAut, lSSL, cServerIp, cImagem, lInformaEnvio )

   LOCAL I, OMSG, X
   LOCAL aTo
   LOCAL lRet
   LOCAL nEle, oErroMail
   LOCAL cImagem1 := ''

   hb_Default( @cImagem, "" )
   hb_Default( @lInformaEnvio, .T. )

// hb_Default(@cMsgTela, 'Enviando Email !!! Aguarde ...')
 
   IF !Empty( cImagem )
      cImagem1 := ''
   ENDIF

   aTo   := { cDest } //--> PARA
 
   nEle := 1

   FOR I := 1 TO Len( aTo )

      TRY

         // MOSTRA_Email(cMsgTela)

         IF lInformaEnvio

            MsgWait( "Aguarde, processando o envio do email." )

         ENDIF

         oMsg := win_OleCreateObject( "CDO.Message" )

         WITH OBJECT oMsg
            :Configuration := oCfg
            :From     := cFrom
            :To      := aTo[i]
            :Cc      := aCC
            :BCC     := aBCC
            :Subject   := cSubject

            // ---------------------------------------------------------
            // Aqui adiciona a imagem ao corpo da mensagem
            // ---------------------------------------------------------
            IF !Empty( cImagem )

               :AddRelatedBodyPart( hb_DirBase() + "img" + hb_PS() + cImagem, cImagem, 1 )
               :Fields:Item( "urn:schemas:mailheader:Content-ID" ):Value := "<" + cImagem + ">"
               :Fields:Item( "urn:schemas:mailheader:Content-Disposition" ):Value := "inline"
               :Fields:Update()

            ENDIF

            :HTMLBody := cMsg // + QuebraHTML + IF(!Empty(cImagem), cImagem1, "")

            FOR X := 1 TO Len( aFiles )
               :AddAttachment( AllTrim( aFiles[x] ) )
               //DO EVENTS
            NEXT

            :Fields( "urn:schemas:mailheader:disposition-notification-to" ):Value := cFrom
            :Fields( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value := lAut
            :Fields( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value := lSSL
            :Fields( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value := cServerIp
            :Fields:update()
            //DO EVENTS
            :Send()
         END WITH

         IF lInformaEnvio

            // MOSTRA_Email("E-mail enviado com sucesso !!!")
            MsgInfo( "E-mail enviado com sucesso!!! Com o Programa do Rubens!" )

            millisec( 500 )
            // HB_ALERT("E-mail enviado com sucesso", "Aten‡Æo")
         ENDIF

         lRet := .T.

      CATCH oErroMail

         IF lInformaEnvio

            MsgStop( "Não foi possível enviar a mensagem: " + cSubject + hb_EOL() + ;
               "para o email: " + aTo[i] + "."                  + hb_EOL() + ;
               "Erro: " + oErroMail:Description , "Atenção" )

         ENDIF

         lRet := .F.
      END

   NEXT
 
   oCfg := Nil
   oMsg := Nil

   RETURN lRet

//*******************************************************************************

FUNCTION MENSAG( cTEXTO )

   RETURN( ALERT( cTEXTO ) )

//*******************************************************************************

FUNCTION StringToArray( cString, cSeparator )

   LOCAL nPos
   LOCAL aString := {}

   cSeparator := ";"
   cString := ALLTRIM( cString ) + cSeparator
   DO WHILE .T.
      nPos := AT( cSeparator, cString )
      IF nPos = 0
         EXIT
      ENDIF
      AADD( aString, SUBSTR( cString, 1, nPos - 1 ) )
      cString := SUBSTR( cString, nPos + 1 )
   ENDDO

   RETURN ( aString )
   
//*******************************************************************************

FUNCTION ArrayToString( aArray, cSeparator )

   LOCAL nPos, cString

   cSeparator := ";"

   cString := ""

   FOR nPos = 1 TO LEN( aArray )
      cString := cString + aArray[nPos] + cSeparator
   NEXT

   RETURN ( cString )

//*******************************************************************************

 
João Santos - São Paulo - Brasil
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: oMail:HTMLBody problem

Post by karinha »

Code: Select all

// Exemplo de como se usa outlook do windows.

#Include "FiveWin.ch"

FUNCTION Main()

   LOCAL _cTo          // Para
   LOCAL _cComCopia    // com copia
   LOCAL _cAttach      // Anexos
   LOCAL _cSubject     // Texto do email
   LOCAL _cBody        // Corpo do Email
   LOCAL _cHtml        // Nao sei quem e...
   LOCAL _nProvedor    // nao sei quem e

   _cTo       := "kapiabafwh@gmail.com"

   _cComCopia := "kapiabafwh@bol.com.br"

   _cAttach   := ""  // Mostre como anexar

   _cSubject  := "Envio do Arquivo Xml da Nfe"

   _cBody     := "Corpo do Email usando outlook"

   _cHtml     := "" // Nao sei quem e...

   _nProvedor := "" // nao sei quem e

   Marca_EnvioEmailOutLook( _cTo, _cComCopia, _cAttach, _cSubject, _cBody, ;
                            _cHtml, _nProvedor )


RETURN NIL

****************************************************************************************************************************************************************
FUNCTION Marca_EnvioEmailOutLook( _cTo, _cComCopia, _cAttach, _cSubject, _cBody, _cHtml, _nProvedor )
****************************************************************************************************************************************************************

   LOCAL oOutlook, oMail, oPub //??
   LOCAL nEle
   LOCAL cArquivoAtach

   TRY

      oOutLook := GetActiveObject( "Outlook.Application" )

   CATCH

      TRY

         oOutLook := CREATEOBJECT("Outlook.Application")
         * Ver a necessecidade desta linha abaixo
         *oNameSpace := oOutlook:GetNameSpace("MAPI")

      CATCH

         MsgInfo("Não foi possivel encontrar o Microsoft Outlook Instalado, Favor revisar","ATENÇÃO")

         RETURN .F.

      END

   END

   // oMail:Display := .T. Abre a tela do Microsoft Outlook antes de enviar
   // oMail:Display := .F. Pisca a tela do outlook mais nao abre e vai direto pro emsil
   // Caso comente esta variavel vai direto pro outlook sem chamar tela nenhuma e envia o email

   oMail         := oOutLook:CreateItem( 0 )
   oMail:Subject := Alltrim(_cSubject)
   oMail:Body    := AllTrim(_cBody)
   _cTo          := AllTrim(StrTran(_cTo,",",";" ))
   oMail:To      := _cTo

   * Tambem pode ser usado assim
   *nEle := 1
   *While !Empty( cTo := StrToken( _cTo, nEle++, ";" ) )

   *  oMail:Recipients:Add( Alltrim( cTo ) )
   *  SysRefresh()

   *EndDo

   IF !Empty( _cAttach )  // Anexos aqui

      nEle := 1

      While !Empty( cArquivoAtach := StrToken( _cAttach, nEle++, "," ) )

         oMail:Attachments:Add( AllTrim(cArquivoAtach) )

      EndDo

   Endif

   IF !Empty ( _cComCopia )

      oMail:CC := Alltrim(_cComCopia)

   ENDIF

   // nao entendi isso, nem tenho...
   IF File(oPub:wPathSys + "ACOMPANHAEMAIL.ARQ") .AND. _nProvedor = 1

      IF !Empty(Setup->EmailUser)

         // Copia Oculta
         oMail:BCC := AllTrim(Setup->EmailUser)

      ENDIF

   ENDIF

   IF !Empty( _cHtml )
      // Testar com html
      // Dica http://fivewin.com.br/index.php?/topic/24468-falta-pouco-para-o-email-ficar-legal-alguem-ajuda/#comment-273115

      oMail:HTMLBody := MEMOREAD(ALLTRIM(_cHtml))

   ENDIF

   oMail:Send()

RETURN .T.
 
João Santos - São Paulo - Brasil
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: oMail:HTMLBody problem

Post by karinha »

João Santos - São Paulo - Brasil
Marc Vanzegbroeck
Posts: 1102
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium
Contact:

Re: oMail:HTMLBody problem

Post by Marc Vanzegbroeck »

The problem is solved, after updating Windows 8)
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Post Reply