Page 1 of 1

Email from gmail

Posted: Thu Dec 10, 2020 1:37 pm
by Marc Vanzegbroeck
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?

Re: Email from gmail

Posted: Thu Dec 10, 2020 1:47 pm
by Rick Lipkin
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

Re: Email from gmail

Posted: Fri Dec 11, 2020 9:38 am
by Marc Vanzegbroeck
Rick,

Thank you for reminding me.

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

Re: Email from gmail

Posted: Mon Dec 14, 2020 3:57 pm
by mariordz
Rick, eventough I activated the "less secure apps" option, I still get the same error when sending e-mail.

Any other posiblle reason?

Image
Image

Code: Select all

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

Re: Email from gmail

Posted: Mon Dec 14, 2020 4:04 pm
by Enrico Maria Giordano
Any chance for me to have the password to make some test here?

EMG

Re: Email from gmail

Posted: Mon Dec 14, 2020 5:02 pm
by mariordz
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.

Re: Email from gmail

Posted: Mon Dec 14, 2020 6:04 pm
by Enrico Maria Giordano
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: Select all

#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:schemas:mailheader:disposition-notification-to" ):Value = cFrom
            oMsg:Fields:Update()
        ENDIF

        oMsg:Send()

        lOk = .T.
    CATCH
    END

    RETURN lOk
EMG

Re: Email from gmail

Posted: Mon Dec 14, 2020 10:53 pm
by mariordz
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.

Re: Email from gmail

Posted: Tue Dec 15, 2020 2:58 am
by hua
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

Re: Email from gmail

Posted: Tue Dec 15, 2020 8:54 am
by Enrico Maria Giordano
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

Re: Email from gmail

Posted: Tue Dec 15, 2020 8:58 am
by Enrico Maria Giordano
Follow the instruction for SSL here:

http://forums.fivetechsupport.com/viewt ... 8&start=15

EMG

Re: Email from gmail

Posted: Tue Dec 15, 2020 9:15 am
by Baxajaun
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,

Re: Email from gmail

Posted: Tue Dec 15, 2020 10:18 am
by hua
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