Email from gmail

Email from gmail

Postby Marc Vanzegbroeck » Thu Dec 10, 2020 1: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Email from gmail

Postby Rick Lipkin » Thu Dec 10, 2020 1: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
User avatar
Rick Lipkin
 
Posts: 2663
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Email from gmail

Postby Marc Vanzegbroeck » Fri Dec 11, 2020 9: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Email from gmail

Postby mariordz » Mon Dec 14, 2020 3: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?

Image
Image

Code: Select all  Expand view
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: 131
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Email from gmail

Postby Enrico Maria Giordano » Mon Dec 14, 2020 4:04 pm

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

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Email from gmail

Postby mariordz » Mon Dec 14, 2020 5: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.
User avatar
mariordz
 
Posts: 131
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Email from gmail

Postby Enrico Maria Giordano » Mon Dec 14, 2020 6: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: Select all  Expand view
#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
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Email from gmail

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

Re: Email from gmail

Postby hua » Tue Dec 15, 2020 2: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
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Email from gmail

Postby Enrico Maria Giordano » Tue Dec 15, 2020 8: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
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Re: Email from gmail

Postby Baxajaun » Tue Dec 15, 2020 9: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,
User avatar
Baxajaun
 
Posts: 968
Joined: Wed Oct 19, 2005 2:17 pm
Location: Gatika. Bizkaia

Re: Email from gmail

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


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 73 guests