Page 1 of 2

SMTP AUTH example

Posted: Sun Oct 31, 2004 2:15 pm
by rob6523
Does anyone have a simple SMPT AUTH send mail example for me?

I've been looking around but haven't found anything that does the job.
Including the blat.dll version.

Rob.

Posted: Sun Oct 31, 2004 4:05 pm
by GeoTrail
Check out the PureSMTP library.
You'll find it here on the forum.

Posted: Sun Oct 31, 2004 5:04 pm
by rob6523
Thanks,

unfortunately this lib doesn't do the job for me.
It doesn't connect to the pop and smtp server (returns error code) although I've entered the correct info.

Rob.

Posted: Sun Oct 31, 2004 7:15 pm
by plouf
PureSMTP do not suport SMTP conection with AUTH as i see in latest vesrion at least

Posted: Sun Oct 31, 2004 9:00 pm
by GeoTrail
Yes it does

Code: Select all

PureSMTP_OpenPOP3ThenSMTPConnection(POP3Server.s, POP3Port.l, POP3UserName.s, POP3UserPassword.s, SMTPServer.s, SMTPPort.l) : Open connection to POP3 server (authentification) and then to SMTP server : returns #PureSMTP_Ok if success (else is error code)

Posted: Sun Oct 31, 2004 9:18 pm
by plouf
this is not smtp auth

but an authorising based on conecting to pop3 server fisrt
its a method used by a few free mail providers to allow you

HEre is an page explaining PLAIN AUTH (google search)

http://www.technoids.org/saslmech.html

gmx smtp support PLAIN AUTH (just test with telnet)

Posted: Sun Oct 31, 2004 9:21 pm
by GeoTrail
Well it works for my smtp, and it is not free ;)

Posted: Sun Oct 31, 2004 9:34 pm
by plouf
you missunderstood my words

i did not said that it did not work, but what i said was
that AUTH LOGIN is a specific protocol negotiation <a href='http://www.ietf.org/rfc/rfc2554.txt'>extensions</a> with smtp server
and this negotiation is not supported by PureSMTP library

Posted: Sun Oct 31, 2004 9:36 pm
by GeoTrail
Oh, ok. Sorry 'bout that :)

Posted: Mon Nov 01, 2004 2:09 am
by rob6523
Hi Plouf,

You are very much right!
Thanks for the information.
This is exactly my problem.

Rob.

Posted: Mon Nov 01, 2004 2:42 am
by rob6523
I have a piece of powerbasic code that covers the smtp auth process but I'm unable to port it to purebasic....
Note that this is no complete smtp code but only complete on the smtp auth process....
Perhaps one of you can have a go at it...

'this is the main code section
'-----------------------------------------------------------
HOST NAME TO localhost
TCP OPEN "smtp" AT SmtpHost AS hTCP
TCP_LINE hTCP, sLine
e = VAL(LEFT$(sLine, 3))
IF e = 220 THEN
TCP_PRINT hTCP, "EHLO " & localhost
DO WHILE NOT EOF(hTCP)
TCP_LINE hTCP, sLine
e = VAL(LEFT$(sLine, 3))
LOOP
IF e <> 250 THEN
'no reply
ELSE
Authentication hTCP, e
'from here the coding is the same as usual
END IF
ELSE
'server down
END IF
'------------------------------------------------------------
'end of main code section
'------------------------------------------------------------
'------------------------------------------------------------
'code fore authentication
'------------------------------------------------------------
SUB Authentication(BYVAL hTCP AS LONG, e AS LONG)
DIM mTmp AS STRING
DIM Buffer AS STRING
DO WHILE NOT EOF(hTCP)
TCP LINE hTCP, Buffer
e = VAL(LEFT$(Buffer, 3))
LOOP
TCP PRINT hTCP, "AUTH LOGIN"
DO WHILE NOT EOF(hTCP)
TCP LINE hTCP, Buffer
e = VAL(LEFT$(Buffer, 3))
LOOP
IF e = 334 THEN
mTmp = StringToMime(secUser) 'secUser is the UserId (might be the same as the POP3 UserId)
TCP PRINT hTCP, mTmp
DO WHILE NOT EOF(hTCP)
TCP LINE hTCP, Buffer
e = VAL(LEFT$(Buffer, 3))
LOOP
IF e = 334 THEN
mTmp = StringToMime(secPassword) 'secPassword is the password (might be the same as the POP3 password)
TCP PRINT hTCP, mTmp
DO WHILE NOT EOF(hTCP)
TCP LINE hTCP, Buffer
e = VAL(LEFT$(Buffer, 3))
LOOP
END IF
END IF
END SUB
'this code is by Dave Navarro
FUNCTION StringToMIME(BYVAL InBuff AS STRING) AS STRING
LOCAL Enc AS STRING * 64
LOCAL b AS ASCIIZ * 4
LOCAL OutBuff AS STRING
REGISTER i AS LONG
Enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
WHILE LEN(InBuff)
b = LEFT$(InBuff, 3)
! mov AL, b[0]
! shr AL, 2
! movzx i, AL
OutBuff = OutBuff + MID$(Enc, i + 1, 1)
! mov AL, b[1]
! mov AH, b[0]
! shr AX, 4
! and AL, &H3F
! movzx i, AL
OutBuff = OutBuff + MID$(Enc, i + 1, 1)
IF LEN(InBuff) = 1 THEN
OutBuff = OutBuff + "=="
EXIT DO
END IF
! mov AL, b[2]
! mov AH, b[1]
! shr AX, 6
! and AL, &H3F
! movzx i, AL
OutBuff = OutBuff + MID$(Enc, i + 1, 1)
IF LEN(InBuff) = 2 THEN
OutBuff = OutBuff + "="
EXIT DO
END IF
! mov AL, b[2]
! and AL, &H3F
! movzx i, AL
OutBuff = OutBuff + MID$(Enc, i + 1, 1)
InBuff = MID$(InBuff, 4)
WEND
FUNCTION = OutBuff
END FUNCTION
'------------------------------------------------------------
'end of authentication
'------------------------------------------------------------



Rob.

Send Mails SMTP-AUTH or Regular and LARGE Attachments > 6

Posted: Thu Dec 23, 2004 1:52 pm
by clipper
This Code works for me, also with large Attachments.
You can send your mail with SMTP-Auth or - if you leave the Username blank- regular.

To implement POP bevor SMTP as another Login-Mechanism would be easy.

Ok, a timeout would be nice.

Most of the code are snippets I´ve found here

Code: Select all


Global ConnectionID.l
Global CrLf.s
CrLf.s=Chr(13)+Chr(10)

Enumeration 
#eHlo
#RequestAuthentication
#Username
#Password
#MailFrom
#RcptTo
#Data
#Quit
#Complete
EndEnumeration

 NewList Attachments.s() 
 InsertElement(Attachments()) 
 Attachments() = "c:\afile.htm"
;InsertElement(Attachments()) 
;Attachments() = "c:\another.jpg" 

Declare.s Base64Encode(strText.s)
Declare SendFiles()
Declare.s GetMIMEType(Extension.s) 
Declare Send(msg.s)
Declare SendESMTPMail(name.s,sender.s,recipient.s,username.s,password.s,smtpserver.s,subject.s,body.s)



;Sending Mail with SMTP-AUTH
sendesmtpmail("Clipper","my@email.com","your@email.com","username","password","auth.smtp.mailserver.com","Hallo","This is the body")


; Don´t fill the Username if you want to sent regular
;sendesmtpmail("Clipper","my@email.com","your@email.com","","","smtp.mailserver.com","Hallo","This is the body")

Procedure SendESMTPMail(name.s,sender.s,recipient.s,username.s,password.s,smtpserver.s,subject.s,body.s)
If InitNetwork()
   ConnectionID = OpenNetworkConnection(smtpserver, 25)
   If ConnectionID
      loop250.l=0
      Repeat   
         If NetworkClientEvent(ConnectionID)
            ReceivedData.s=Space(9999)
            ct=ReceiveNetworkData(ConnectionID ,@ReceivedData,9999) 
            If ct
               cmdID.s=Left(ReceivedData,3)
               cmdText.s=Mid(ReceivedData,5,ct-6)
               Debug "<" + cmdID + " " + cmdText
               Select cmdID
                  Case "220"
                     If Len(username)>0
                        Send("Ehlo " + Hostname())
                        state=#eHlo
                     Else
                        send("HELO " + Hostname())
                        state=#MailFrom
                     EndIf   
                  Case "221"
                     send("[connection closed]")
                     state=#Complete
                     quit=1      
                  Case "235"
                     Send("MAIL FROM: <" + sender + ">")
                     state=#RcptTo
                    
                  Case "334"
                     If state=#RequestAuthentication
                        Send(Base64Encode(username))
                        state=#Username
                     EndIf
                     If state=#Username
                        Send(Base64Encode(password))
                        state=#Password
                     EndIf
 
                  Case "250" 
                     Select state
                        Case #eHlo
                           send("AUTH LOGIN")
                           state=#RequestAuthentication     
                        Case #MailFrom   
                           Send("MAIL FROM: <" + sender + ">")
                           state=#RcptTo
                        Case #RcptTo
                           Send("RCPT TO: <" + recipient + ">")
                           state=#Data
                        Case #Data
                           Send("DATA")
                           state=#QUIT
                        Case #QUIT
                           Send("QUIT")
                     EndSelect
              
                  Case "251"
                        Send("DATA")
                        state=#Data
                  Case "354"
                     send("X-Mailer: eSMTP 1.0")
                     send("To: " + recipient)
                     send("From: " + name + " <" + sender + ">")
                     send("Reply-To: "+sender)
                     send("Date:" + FormatDate("%dd/%mm/%yyyy @ %hh:%ii:%ss", Date()) )
                     send("Subject: " + Subject)
                     send("MIME-Version: 1.0")
                     send("Content-Type: multipart/mixed; boundary="+Chr(34)+"MyBoundary"+Chr(34))
                     Send("")
                     send("--MyBoundary")
                     Send("Content-Type: text/plain; charset=us-ascii")
                     Send("Content-Transfer-Encoding: 7bit")
                     send("")                     
                     Send(body.s)
                     SendFiles()
                     send("--MyBoundary--")
                     Send(".")
              
                  Case "550"
                       
                     quit=1      
               EndSelect
            EndIf 
         EndIf
         
      Until Quit = 1
      CloseNetworkConnection(ConnectionID)
      MessageRequester("","Ende")
   EndIf
EndIf          
EndProcedure 

Procedure Send(msg.s)
;Delay(10)
Debug "> " + msg
msg+crlf.s
SendNetworkData(ConnectionID, @msg, Len(msg))
EndProcedure 


Procedure SendFiles()
ResetList(Attachments())
While(NextElement(Attachments()))
file.s=Attachments()
Send("")
If ReadFile(0,file.s) 
   Debug file
   InputBufferLength.l = Lof()
   OutputBufferLength.l = InputBufferLength * 1.4
   *memin=AllocateMemory(InputBufferLength) 
   If *memin
      *memout=AllocateMemory(OutputBufferLength)
      If *memout
         Boundry.s = "--MyBoundary"
         Send(Boundry)
         Send("Content-Type: "+GetMIMEType(GetExtensionPart(file.s)) + "; name=" + Chr(34) + GetFilePart(file.s) + Chr(34)) 
         send("Content-Transfer-Encoding: base64")
         send("Content-Disposition: Attachment; filename=" + Chr(34) + GetFilePart(file) + Chr(34))
         send("")
         ReadData(*memin,InputBufferLength)
         Base64Encoder(*memin,60,*memout,OutputBufferLength)
         send(PeekS(*memout,60)) ; this must be done because For i=0 To OutputBufferLength/60 doesn´t work
         Base64Encoder(*memin,InputBufferLength,*memout,OutputBufferLength)                
         For i=1 To OutputBufferLength/60
             temp.s=Trim(PeekS(*memout+i*60,60))
             If Len(temp)>0
              send(temp)
             EndIf 
         Next
      EndIf
   EndIf
   FreeMemory(-1)
   CloseFile(0)
EndIf 
Wend
ProcedureReturn
EndProcedure 


Procedure.s Base64Encode(strText.s) 
    DefType.s Result 
    *B64EncodeBufferA = AllocateMemory(Len(strText)+1) 
    *B64EncodeBufferB = AllocateMemory((Len(strText)*3)+1) 
    PokeS(*B64EncodeBufferA, strText) 
    Base64Encoder(*B64EncodeBufferA, Len(strText), *B64EncodeBufferB, Len(strText)*3) 
    Result = PeekS(*B64EncodeBufferB) 
    FreeMemory(-1)
    ProcedureReturn Result 
EndProcedure


Procedure.s GetMIMEType(Extension.s) 
    Extension = "." + Extension 
    hKey.l = 0 
    KeyValue.s = Space(255) 
    DataSize.l = 255 
    If RegOpenKeyEx_(#HKEY_CLASSES_ROOT, Extension, 0, #KEY_READ, @hKey) 
        KeyValue = "application/octet-stream" 
    Else 
        If RegQueryValueEx_(hKey, "Content Type", 0, 0, @KeyValue, @DataSize) 
            KeyValue = "application/octet-stream" 
        Else 
            KeyValue = Left(KeyValue, DataSize-1) 
        EndIf 
        RegCloseKey_(hKey)
    EndIf 
    ProcedureReturn KeyValue 
EndProcedure


..

Posted: Sun May 22, 2005 2:12 pm
by NoahPhense
This code kicks as$ .. JUST WHAT I NEEDED!! THANKS

- np

Posted: Sun May 29, 2005 6:51 am
by rob6523
Thanks Clipper!

Works fine.

Rob. :lol:

Posted: Mon May 30, 2005 8:22 am
by gnozal
plouf wrote:... AUTH LOGIN is a specific protocol negotiation <a href='http://www.ietf.org/rfc/rfc2554.txt'>extensions</a> with smtp server and this negotiation is not supported by PureSMTP library
SMTP AUTH LOGIN is supported by PureSMTP (at least, it works for me).