Email from gmail

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

Email from gmail

Post 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?
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Email from gmail

Post 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
Marc Vanzegbroeck
Posts: 1102
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium
Contact:

Re: Email from gmail

Post by Marc Vanzegbroeck »

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
User avatar
mariordz
Posts: 127
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Email from gmail

Post 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
User avatar
mariordz
Posts: 127
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Email from gmail

Post 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.
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Email from gmail

Post 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
User avatar
mariordz
Posts: 127
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Email from gmail

Post 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.
hua
Posts: 861
Joined: Fri Oct 28, 2005 2:27 am

Re: Email from gmail

Post 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
FWH 11.08/FWH 19.03
xHarbour 1.2.1 (Rev 6406) + BCC
Harbour 3.1 (Rev 17062) + BCC
Harbour 3.2.0dev (r1904111533) + BCC
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Email from gmail

Post 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
User avatar
Baxajaun
Posts: 853
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: Email from gmail

Post 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,
hua
Posts: 861
Joined: Fri Oct 28, 2005 2:27 am

Re: Email from gmail

Post 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
FWH 11.08/FWH 19.03
xHarbour 1.2.1 (Rev 6406) + BCC
Harbour 3.1 (Rev 17062) + BCC
Harbour 3.2.0dev (r1904111533) + BCC
Post Reply