TIpClientSmtp for Gmail

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

TIpClientSmtp for Gmail

Post by danielmaximiliano »

Carlos Reis wrote:Hello Daniel
the user and password that I used in the program was just an example obviously does not correspond to reality.
could still have further simplified the example of routine because the aim was to hb_SendMail function ()
that it works well in version 3.0.35 HMG but does not work in version 3.0.46 HMG
down I enclose a version of the routine further simplified only to test the sending of e-mail now with real user and password
Hi Carlos :
Try

Code: Select all

#include 'hmg.ch'
#require "hbtip"

Function Main
 Local oConnection , oMail
 Local cAssunto := "Aviso de chegada de material"
 Local cTo      := 'carlos.reis@canas.pt'
 Local cCorpo   := 'Serve o presente e-mail para informar de que se encontra em armazem  o seguinte material : '
 
 /* Create connection on mail server smtp://<mailAccount>:<password>@<mail.server.com>    */
 /* .T. Trace in smtp-x.log                                                               */
 oConnection              := TIpClientSmtp():new( "smtp://pedro.silva256:pedro256@smtp.gmail.com" , .T. )  
 
 
       IF .NOT. oConnection:open()
        MsgInfo( "Connection error : " + oConnection:lastErrorMessage()+ chr(10) +;
                 "Try again or bad Pass/User "  ,     " ¡¡  Message Error  !! "   )
        Quit
      Endif
       ? "Connection Alive  Message Attention !!!! " 
        
 /* Create Email */        
 oMail := TIpMail():new( hb_StrToUTF8( cCorpo )  )
 oMail:hHeaders[ "Content-Type" ] := "text/plain; charset=utf-8"
 oMail:hHeaders[ "Date" ]    := tip_TimeStamp()
 oMail:hHeaders[ "From" ]    := hb_StrToUTF8( cOrigen )
 oMail:hHeaders[ "To" ]      := hb_StrToUTF8( cTo )
 oMail:hHeaders[ "Subject" ] := hb_StrToUTF8( cAssunto )
 /* create attach */
 
 /* end attach    */
 
 
 oConnection:sendMail( oMail )
RETURN



probe with my account and it works, try with your account and also receive the email.

need to create the inclusion of multiple attachments and problem solved.

http://www.arclab.com/products/amlc/lis ... -list.html
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: TIpClientSmtp for Gmail

Post by danielmaximiliano »

danielmaximiliano wrote: need to create the inclusion of multiple attachments and problem solved.
Hi :
Translate google :
I solved part of TipMail File Inclusion.
Selecting a file that is sent without compression, but if you select multiple files these are compressed by hbmzip, it is created in the folder where the application is launched.
still have not found it does not attach the Zip file and any other ZIP, you may have a lot of sleep and, if anyone wants to correct this problem would be great.

Español :
Resolvi parte de la inclusión de archivos en TipMail.
al Seleccionar un archivo este se envia sin compresion, pero si se selecciona varios archivos estos se comprimen mediante hbmzip, el mismo se crea en la carpeta donde se lanza la aplicacion.
todavia no encontre porque no se adjunta el Zip y cualquier otro archivo ZIP, puede ser que tenga mucho sueño ya, si alguno quiere corregir este problema sería muy bueno.

Code: Select all

#include 'hmg.ch'
#require "hbtip"
#include "hbmzip.ch"


Function Main
 Public lSend         := .F.
 Public cSubject      := "TIpClientSmtp()"
 Public cFrom         := 'userid'
 Public cTo           := 'userid@server.com' //
 Public cBody         := 'This is a test mail sent at : ' + DtoC(date()) + ' ' + Time()
 Public oConnection , oMail , oAttachments
 Public cUserId , cPassword , cUrl    , cProto    , cServer := ''
 Public cFilePath , cFileName , cFileExt
 Public CurDirectory := Curdir()
 
 cProto    := "smtp://"
 cServer   := "smtp.gmail.com"
 cUserId   := "UserID"
 cPassword := "UserPass"
 cUrl      := cProto + cUserId + ":" + cPassword + "@" + cServer 
 
 /* .T. Trace in smtp-x.log                                                               */
 oConnection              := TIpClientSmtp():new( cUrl , .T. )  
 
 IF .NOT. oConnection:open()
    MsgInfo( "Connection error : " + oConnection:lastErrorMessage()+ chr(10) +;
             "Try again or bad Pass/User "  ,     " ¡¡  Message Error  !! "   )
    Quit
 Endif
 
 If ( oConnection:auth( cUserId , cPassword ) )
      
    /* Create Email     */ 
    oMail := TIpMail():new( hb_StrToUTF8( cBody )  )
    oMail:hHeaders[ "Content-Type" ] := "text/plain; charset=utf-8"
    oMail:hHeaders[ "Date" ]    := tip_TimeStamp()
    oMail:hHeaders[ "From" ]    := hb_StrToUTF8( cFrom )
    oMail:hHeaders[ "To" ]      := hb_StrToUTF8( cTo )
    oMail:hHeaders[ "Subject" ] := hb_StrToUTF8( cSubject )
    /* create attach */
    If MsgYesNo("¿ Attach Files ?")
       AddAttachment() 
    Endif
    /* Send Email */
    lSend := oConnection:sendMail( oMail )
 
    If lSend
       MsgInfo( "Send Sucefully : " ,  " ¡¡  Message Info !! "   )
    Else
       MsgInfo( "Send Failed : " + oConnection:lastErrorMessage() + chr( 13 ) + chr(10) +;
                "Try again  "  ,     " ¡¡  Message Error  !! "   )
      Quit
    endif
 Else /* Fail Auth */
   MsgInfo( "Auth Failed : " + oConnection:lastErrorMessage() + chr( 13 ) + chr(10) +;
          "Try again, Bad Id or Pass   "  ,     " ¡¡  Message Error  !! "   )
 Endif 
 
RETURN Nil 

 *------------------------------------------*
 function AddAttachment()
 *------------------------------------------*
	local aFiles  , aArchives     := {}
	local nI , hZip ,  cArq 
    Local cComment    := "Multiple Files in Zip"	
    Local FileNameZip := DiskName() + ":\" + CurDirectory + "\Attachment.zip"
    /* Search Files */
	aFiles := Getfile ( { {'All Files','*.*'} } , 'Get attachment' , 'c:\' , .t. , .t. )
	For nI:=1 to len( aFiles )
	   hb_FNameSplit( aFiles [ nI ] ,@cFilePath , @cFileName, @cFileExt ) 
       aadd(aArchives , cFilePath + cFileName + cFileExt )
    next
    
	If len( aFiles ) > 0
	   If len( aFiles ) == 1
	      Mailattachment( cFilePath + cFileName + cFileExt  )
	   Else
		  hZip := HB_ZIPOPEN( FileNameZip )
		  FOR EACH cArq IN aArchives
	          HB_ZipStoreFile( hZip, cArq , cComment)
          NEXT
       HB_ZipFileClose( hZip )  
       Mailattachment( FileNameZip )
	   Endif    
       oMail:attach( oAttachments )  
	Endif
		
 Return 
 
 *------------------------------------------*
 Function Mailattachment( cData )
 *------------------------------------------*
   // Create Attachment
 msginfo( "Send : " + cData )
 oAttachments := TIpMail():new()
   // TODO: mime type magic auto-finder
 oAttachments:SetEncoder( "base64" )
  // Some EMAIL readers use Content-Type to check for filename
 oAttachments:hHeaders[ "Content-Type" ] := ;
                        "application/X-TIP-Attachment; filename=" + cData
                                           
   // But usually, original filename is set here
 oAttachments:hHeaders[ "Content-Disposition" ] := "attachment; filename=" + cData
 
 oAttachments:SetBody( cData )
 Return
 
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
hmgchang
Posts: 273
Joined: Tue Aug 13, 2013 4:46 am
Location: Indonesia

Re: TIpClientSmtp for Gmail

Post by hmgchang »

Code: Select all

#include 'hmg.ch'
#require "hbtip"
#include "c:\hmg.3.2\harbour\contrib\hbmzip\hbmzip.ch"


Function Main
 Public lSend         := .F.
 Public cSubject      := "TIpClientSmtp()"
 Public cFrom         := "tanikchong" && 'userid'
 Public cTo           := "tanikchong@gmail.com" && 'userid@server.com' //
 Public cBody         := 'This is a test mail sent at : ' + DtoC(date()) + ' ' + Time()
 Public oConnection , oMail , oAttachments
 Public cUserId , cPassword , cUrl    , cProto    , cServer := ''
 Public cFilePath , cFileName , cFileExt
 Public CurDirectory := Curdir()
 
 cProto    := "smtp://"
 cServer   := "smtp.mail.yahoo.com" && "smtp.gmail.com"
 cUserId   := "tanikchong" && "UserID"
 cPassword := "mypassword" && "UserPass"
 cUrl      := cProto + cUserId + ":" + cPassword + "@" + cServer
 
 /* .T. Trace in smtp-x.log                                                               */
 oConnection              := TIpClientSmtp():new( cUrl , .T. ) 
 
 IF .NOT. oConnection:open()
    MsgInfo( "Connection error : " + oConnection:lastErrorMessage()+ chr(10) +;
             "Try again or bad Pass/User "  ,     " ¡¡  Message Error  !! "   )
    Quit
 Endif
I modified your code with my account,
it compiled but get this when run :
Attachments
hbtip2.JPG
hbtip2.JPG (15.37 KiB) Viewed 2994 times
Just Hmg It !
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: TIpClientSmtp for Gmail

Post by danielmaximiliano »

hmgchang wrote: I modified your code with my account,
it compiled but get this when run :
ENETUNREACH
The network of the given addr isn't reachable from this host.
no more information about this type of error in Yahoo mail.

may range from the IP to connect with disabilities have Mail.yahoo.com
You set try if Yahoo Mail has a local server in your area.

in my case yahoo has a local service and my server is.
mail.yahoo.com.ar
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
mol
Posts: 3720
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: TIpClientSmtp for Gmail

Post by mol »

While searching solution for sending attachments via hb_sendmail() (I don't know why it can't send e-mails with attachment, but sends OK without it)
I've studied sources of hptip library.
While creating cURL and connection, you use code

Code: Select all

 cProto    := "smtp://"
 cServer   := "smtp.mail.yahoo.com" && "smtp.gmail.com"
 cUserId   := "tanikchong" && "UserID"
 cPassword := "mypassword" && "UserPass"
 cUrl      := cProto + cUserId + ":" + cPassword + "@" + cServer
 oConnection := TIpClientSmtp():new( cUrl , .T. )
 
it does not work when cuserID contains @ character, eg. mol@pro.onet.pl
in this case, cURL is set to:
"smtp://mol@pro.onet.pl:MyPassword@poczta.onet.pl"
There are 2x @ character.
The solution is to replace @ character with phrase "&at;":

Code: Select all

cUserId := StrTran( cUserId, "@", "&at;" )
build cURL, open connection, and translate connection UserId

Code: Select all

cUrl      := cProto + cUserId + ":" + cPassword + "@" + cServer
build oURL object:

Code: Select all

oUrl := TUrl():New(cUrl , lCreateLogFile := .f.)
and convert UserId back:

Code: Select all

oUrl:cUserid := StrTran( cUserId, "&at;", "@" )
and now create oConnection object:

Code: Select all

oConnection := TIPClientSMTP():New( oUrl, lCreateLogFile )
In sum, whole code sholud looks like this:

Code: Select all

 cProto    := "smtp://"
 cServer   := "smtp.gmail.com"
 cUserId   := UserID
 cPassword := UserPass
 cUserId := StrTran( cUserId, "@", "&at;" )
 cUrl      := cProto + cUserId + ":" + cPassword + "@" + cServer
 oUrl := TUrl():New(cUrl , lCreateLogFile := .f.)
 oUrl := cUserid := StrTran( cUserId, "&at;", "@" )
 oConnection := TIpClientSmtp():new( oUrl , lCreateLogFile  )
Post Reply