hb_SendMail() with lPopAuth

Discuss anything else that does not suite other forums.

Moderator: Rathinagiri

Post Reply
User avatar
AUGE_OHR
Posts: 2096
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

hb_SendMail() with lPopAuth

Post by AUGE_OHR »

hi,

when send with SMTP some Provider need lPopAuth which is Parameter Nr. 16
Parameter 12 is for POP3 Server where need to login to identify.

BUT there is only 1 x Port for SMTP (465/587) and POP3 have other Port than SMTP (995) :!:

so how to handle this with hb_SendMail() :?:

---

i found in "core-master" c:\harbour\contrib\hbtip\mailsend.prg

while i have Problem with Login i can't use this Syntax

Code: Select all

  oUrl1 := TUrl():New( iif( lSSL, "pop3s://", "pop://" ) + cUser + ":" + cPass + "@" + cPopServer + "/" )
  oUrl  := TUrl():New( iif( lSSL, "smtps://", "smtp://" ) + cUser + iif( Empty( cSMTPPass ), "", ":" + cSMTPPass ) + "@" + cServer )
  
i have to set User / Password after connect :?:
this Way it work for me

POP3

Code: Select all

   IF lSSL
      cUrl := "pops://"
   ELSE
      cUrl := "pop://"
   ENDIF
   // add Port for POP3
   cUrl += cPopServer + ":" + LTRIM(STR(nPortPOP3))
   oUrl1 := TUrl():New( cUrl )

   oPop := TIPClientPOP():New( oUrl1, xTrace )
   oPop:oUrl:cUserID   := cUser
   oPop:oUrl:cPassword := cPass
   oPop:nConnTimeout := ntimeout
SMTP

Code: Select all

   IF lSSL
      cUrl := "smtps://"
   ELSE
      cUrl := "smtp://"
   ENDIF
   // this is Port for SMTP
   cUrl += cServer + ":" + LTRIM(STR(nPortSMTP))
   oUrl := TUrl():New( cUrl )
   
   oUrl:cUserID   := cUser
   oUrl:cPassword := cPass
   oUrl:cFile := cTo + ;
now i can send Email via SMTP/SSL :D

here Original MailSend.PRG. please modify it yourself and include parameter for Pop3 Server Port when using lPopAuth
have fun
mailsend.zip
c:\harbour\contrib\hbtip\mailsend.prg ORIGINAL
(3.23 KiB) Downloaded 184 times
have fun
Jimmy
User avatar
AUGE_OHR
Posts: 2096
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: hb_SendMail() with lPopAuth

Post by AUGE_OHR »

hi,

did someone use German "smtp.web.de" :?:

it use Port 578 so this line is not exact

Code: Select all

tssl      := iif(tPortSmtp = 465,.T.,.F.)
but my Problem with "smtp.web.de" is that it use STARTTLS and TLS or SSL (?)
https://hilfe.web.de/pop-imap/einrichtu ... itert.html

i can "see" STARTTLS in Debugger ...
StartTLS.JPG
StartTLS.JPG (136.66 KiB) Viewed 2516 times
but where/when to start :?:

Code: Select all

   BEGIN SEQUENCE WITH __BreakBlock()
      oInmail := TIPClientSMTP():New( oUrl, xTrace,, cClientHost )
   RECOVER
      RETURN .F.
   END SEQUENCE
   oInmail:nConnTimeout := nTimeOut
// try to start TLS    
   IF cServer = "smtp.web.de" .AND. nPortSMTP = 587
      oInMail:lTLS := .T.
      oInmail:STARTTLS()
   ENDIF
   IF ! lNoAuth
// here OK   
      IF oInMail:OpenSecure( , lSSL )
// FAIL, do not go here      
         lAuthTLS := oInMail:lTLS
         IF ( oInMail:lAuthLogin .AND. oInMail:Auth( cUser, cSMTPPass ) ) .OR. ;
            ( oInMail:lAuthPlain .AND. oInMail:AuthPlain( cUser, cSMTPPass ) )
            lConnect := .T.
         ENDIF
      ENDIF
      IF ! lConnect
         oInMail:Close()
         BEGIN SEQUENCE WITH __BreakBlock()
            oInmail := TIPClientSMTP():New( oUrl, xTrace,, cClientHost )
         RECOVER
            RETURN .F.
         END SEQUENCE
         oInmail:nConnTimeout := nTimeOut
      ENDIF
   ENDIF

   IF ! lConnect
      IF ! oInMail:Open( NIL, lAuthTLS )
         oInmail:Close()
// here Debugger get out         
RETURN .F.
      ENDIF
   ENDIF
any Idea how to get it work :idea:
have fun
Jimmy
edk
Posts: 999
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: hb_SendMail() with lPopAuth

Post by edk »

I have create a test account and code below works for me. You need to enable POP3 / SMTP support in your account settings, otherwise the message is:
Sehr geehrter Herr Sendmail,
Sie haben kürzlich versucht, Ihr WEB‌.‌DE Postfach in ein E-Mail-Programm wie z. B. Outlook oder die vorinstallierte Mail App Ihres Smartphones einzubinden. Dieser Versuch ist leider fehlgeschlagen, was folgende Ursache haben kann.


Die für E-Mail-Programme benötigten Übertragungsprotokolle POP3/IMAP/SMTP sind standardmäβig zu Ihrer Sicherheit deaktiviert. So soll verhindert werden, dass Unbefugte unbemerkt über POP3/ IMAP/ SMTP auf Ihr Postfach zugreifen können.

Auch wenn Sie Ihr WEB‌.‌DE Postfach bereits in ein E-Mail-Programm eingebunden haben, dieses aber längere Zeit nicht genutzt haben, werden die Protokolle deaktiviert.


Um die Funktion zu nutzen, müssen Sie die Protokolle erstmalig oder erneut in Ihren Postfach-Einstellungen aktivieren. Eine Schritt-für-Schritt-Anleitung dazu finden Sie in der WEB‌.‌DE Hilfe. Anleitung zum Aktivieren

Alternativ empfehlen wir Ihnen die Browser-Erweiterung WEB‌.‌DE MailCheck oder die WEB‌.‌DE Mail App für Windows Desktop zu verwenden. Auf Ihrem Smartphone nutzen Sie einfach die WEB‌.‌DE Mail App. Diese Applikationen bieten ebenfalls einen direkten Zugriff auf Ihr Postfach, benötigen jedoch nicht die unsicheren Protokolle POP3/IMAP/SMTP.

Ihr
WEB‌.‌DE Kundenmanagement
This is the source of the class: https://github.com/harbour/core/blob/ma ... mtpcli.prg
As you can see, you don't have to manually enable TLS. The OpenSecure() method asks the server if and how to authorize access to it. It is important not to enable SSL for servers that support TLS (line 151 in the class source), it will enable itself (line 171)

Code: Select all

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

REQUEST __HBEXTERN__HBSSL__

Function Main()

LOCAL oClientSmtp, oEMail
LOCAL cSmtpUrl , oUrl 
LOCAL cSubject, cFrom, cTo, cBody, cFile ,cMailAccount, cPassword
      
cMailAccount := 'hmgsendmail@web.de'
cPassword    := 'HMG.p@ss'
     
cSmtpUrl := "smtp://smtp.web.de"
cSubject := "Another test email sender "   
cFrom    := 'hmgsendmail@web.de'
cTo      := 'eduardhmgtest@gmail.com'
cBody    := "This is a test mail sent "
oUrl	 := tUrl():New( cSmtpUrl )
     
     
oEMail := TIpMail():new()
oEMail:hHeaders[ "Content-Type" ] := "text/plain; charset=utf-8"
oEMail:hHeaders[ "Date" ]         := tip_TimeStamp()
oEMail:hHeaders[ "From" ]         := hb_StrToUTF8( cFrom )
oEMail:hHeaders[ "To" ]           := hb_StrToUTF8( cTo )
oEMail:hHeaders[ "Subject" ]      := hb_StrToUTF8( cSubject )
oEMail:SetBody( hb_StrToUTF8( cBody ) + e"\r\n" )
      
oClientSmtp              := TIpClientSmtp():new( oUrl , .T.  )
oClientSmtp:nConnTimeout := 2500      
oClientSmtp:nDefaultPort := 587

IF oClientSmtp:OpenSecure( cSmtpUrl , .F. /* lSSL */)
	    Msgdebug("Has TLS?", oClientSmtp:lTLS, "Has AuthLogin?", oClientSmtp:lAuthLogin , "Has Auth PLAIN?", oClientSmtp:lAuthPlain )
	    IF oClientSmtp:lAuthLogin
	    		IF oClientSmtp:lAuthPlain
	    			oClientSmtp:AuthPlain( cMailAccount, cPassword )
	    		ELSE
	    			oClientSmtp:Auth( cMailAccount, cPassword )
	    		ENDIF
	    	ENDIF
         oClientSmtp:sendMail( oEMail )
         oClientSmtp:close()
         Msgbox( "Mail sent" , "Message information" )
      ELSE
		  Msgbox(  "Error:" + oClientSmtp:lastErrorMessage() ,  "Message information"  )
      ENDIF

RETURN 
*****************************************************************************************
User avatar
AUGE_OHR
Posts: 2096
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: hb_SendMail() with lPopAuth

Post by AUGE_OHR »

hi,
edk wrote: Fri Nov 22, 2019 11:06 am I have create a test account and code below works for me.
YES, that's it :!:
i have to study you Code how it work, THX
have fun
Jimmy
Post Reply