FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Email from gmail
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Email from gmail
Posted: Thu Dec 10, 2020 01:37 PM

Hello,

In my program, the customer can mail invoives, and other documents from the program by creating a PDF, and opening the email-program.

Now I have a customer that use a gmail-acount.
Is there a way to open the gmail site, fill the necessairy fields and attach the document from FW?

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Email from gmail
Posted: Thu Dec 10, 2020 01:47 PM

Marc

Don't forget .. The Gmail account you will be using needs to be set for "Less Secure Apps" before you can connect to the account

https://support.google.com/accounts/ans ... 0255?hl=en

Rick Lipkin

Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: Email from gmail
Posted: Fri Dec 11, 2020 09:38 AM
Rick,

Thank you for reminding me.

There is an API for gmail.
https://developers.google.com/gmail/api/guides
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 131
Joined: Tue Dec 26, 2006 04:50 PM
Re: Email from gmail
Posted: Mon Dec 14, 2020 03:57 PM
Rick, eventough I activated the "less secure apps" option, I still get the same error when sending e-mail.

Any other posiblle reason?




Code (fw): Select all Collapse
Function envia_emailext()
   Local oEmailCfg,oEmailMsg
    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 :=  .F.
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value :=  "micuenta@gmail.com"
         :Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value :=  "mipassword"
         :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


Uso Fivewin 19.12, Harbour 3.2.0, BCC77
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Email from gmail
Posted: Mon Dec 14, 2020 04:04 PM

Any chance for me to have the password to make some test here?

EMG

Posts: 131
Joined: Tue Dec 26, 2006 04:50 PM
Re: Email from gmail
Posted: Mon Dec 14, 2020 05:02 PM

Enrico many thanks for answering, for testing purpposes I created the account: tubelitefacturas@gmail.com and the password is SendCFDI_1

I tried sendig one mail with this account and I got the same result.

Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Email from gmail
Posted: Mon Dec 14, 2020 06:04 PM
This is working fine (you can use HB_SENDMAIL() or SENDMAIL()). Please note that you need SSL with [x]Harbour for HB_SENDMAIL() to work.

Code (fw): Select all Collapse
#include "Fivewin.ch"


FUNCTION MAIN()

    LOCAL cFrom     := "tubelitefacturas@gmail.com"
    LOCAL cServer   := "smtp.gmail.com"
    LOCAL cTo       := "e.m.giordano@emagsoftware.it"
    LOCAL cSubject  := "Test message"
    LOCAL cMessage  := "This is a test message."
    LOCAL cUser     := "tubelitefacturas@gmail.com"
    LOCAL cPassword := "SendCFDI_1"
    LOCAL cPort     := "465"
    LOCAL lSSL      := .T.

//    ? HB_SENDMAIL( cServer, VAL( cPort ), cFrom, { cTo }, , , cMessage, cSubject, , cUser, cPassword, , , , , , , , , lSSL )
    ? SENDMAIL( cFrom, cServer, cTo, cSubject, cMessage, , , cUser, cPassword, , , cPort, , lSSL )

    RETURN NIL


#command IF <condition> THEN <*statements*> => if <condition> ; <statements> ; end


STATIC FUNCTION SENDMAIL( cFrom, cServer, cTo, cSubject, cMessage, aAttach, cSender, cUser, cPassword, aCc, lHtml, cPort, lNotification, lSSL )

    LOCAL lOk := .F.

    LOCAL oCfg, oMsg

    LOCAL cCc := ""

    LOCAL i

    DEFAULT lHtml         := "<html" $ LOWER( cMessage )
    DEFAULT lNotification := .F.
    DEFAULT lSSL          := .F.

    TRY
        oCfg = CREATEOBJECT( "CDO.Configuration" )

        oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ):Value = cServer

        IF !EMPTY( cPort )
            oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ):Value := VAL( cPort )
        ENDIF

        oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ):Value = 2
        oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value = .F.

        IF !EMPTY( cUser ) .AND. !EMPTY( cPassword )
            oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ):Value = .T.
            oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ):Value = cUser
            oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ):Value := cPassword
            oCfg:Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ):Value = lSSL
        ENDIF

        oCfg:Update()

        oMsg = CREATEOBJECT( "CDO.Message" )

        oMsg:Configuration = oCfg

        IF !EMPTY( cSender ) THEN cFrom = ["] + cSender + ["] + " <" + cFrom + ">"

        oMsg:From    = cFrom
        oMsg:To      = cTo
        oMsg:Subject = cSubject

        IF !EMPTY( aCc )
            FOR i = 1 TO LEN( aCc )
                IF i > 1 THEN cCc += ";"
                cCc += aCc[ i ]
            NEXT

            oMsg:CC = cCc
        ENDIF

        IF !lHtml
            oMsg:TextBody = cMessage
        ELSE
            oMsg:HTMLBody = cMessage
        ENDIF

        IF !EMPTY( aAttach )
            FOR i = 1 TO LEN( aAttach )
                oMsg:AddAttachment( aAttach[ i ] )
            NEXT
        ENDIF

        IF lNotification
            oMsg:Fields:Item( "urn:mailheader:disposition-notification-to" ):Value = cFrom
            oMsg:Fields:Update()
        ENDIF

        oMsg:Send()

        lOk = .T.
    CATCH
    END

    RETURN lOk


EMG
Posts: 131
Joined: Tue Dec 26, 2006 04:50 PM
Re: Email from gmail
Posted: Mon Dec 14, 2020 10:53 PM

Enrico, I tried your example and it worked only once, I was able to succesfully send one email and then I got the same message again (I chaged the account's password, but I don't see this as the reason of the failure).

I am using another email address (from a secondary domain we have) and it is working apparently good enough. I didn't want to use this account because it fails sometimes and the support I got from the hosting company is not good at all.

I'll keep trying and check if it is not my firewall blocking ao something like that.

Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Re: Email from gmail
Posted: Tue Dec 15, 2020 02:58 AM

I only have experience using blat + GMail.

If the GMail account has 2-step verification enabled, I had needed to create an app password at GMail to allow blat to successfully login to send out email

Not sure if this fact is relevant here or not

FWH 11.08/FWH 19.12

BCC5.82/BCC7.3

xHarbour/Harbour
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Email from gmail
Posted: Tue Dec 15, 2020 08:54 AM
mariordz wrote:Enrico, I tried your example and it worked only once, I was able to succesfully send one email and then I got the same message again (I chaged the account's password, but I don't see this as the reason of the failure).

I am using another email address (from a secondary domain we have) and it is working apparently good enough. I didn't want to use this account because it fails sometimes and the support I got from the hosting company is not good at all.

I'll keep trying and check if it is not my firewall blocking ao something like that.


Did you also try with HB_SENDMAIL()? Please don't forget to activate SSL with [x]Harbour.

EMG
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Posts: 990
Joined: Wed Oct 19, 2005 02:17 PM
Re: Email from gmail
Posted: Tue Dec 15, 2020 09:15 AM
hua wrote:I only have experience using blat + GMail.

If the GMail account has 2-step verification enabled, I had needed to create an app password at GMail to allow blat to successfully login to send out email

Not sure if this fact is relevant here or not


Hi hua !

Could you explain better your app password at GMail ?

Thanks in advance.

Regards,
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Re: Email from gmail
Posted: Tue Dec 15, 2020 10:18 AM
Baxajaun wrote:
Could you explain better your app password at GMail ?


Can read about it here https://support.google.com/accounts/answer/185833?hl=en and here https://devanswers.co/create-applicatio ... ord-gmail/

HTH
FWH 11.08/FWH 19.12

BCC5.82/BCC7.3

xHarbour/Harbour

Continue the discussion