B4J Question Detect when Smtp connection is closed

juventino883

Member
Licensed User
Longtime User
Hi, I'm working on a platform where we have to send several notification emails to our users and I'm using jNet library to do that, in my approach I'm initializing the smtp connection once and after that I start to send emails to our users, the problem is that the mail server can close the connection after some innactive time, how can I detect that the smtp connetion was closed in manner to reinitialize the smtp connection?, there is any example of that?

this is an example of the code that I'm using
example of code:
sub class_globals
    dim smtp as SMTP
    dim newMail, busy as boolean = false
    dim T1 as timer
end sub

Public Sub Initialize
    smtp.Initialize("xxx.xxx.xxx.xxx", port, "xxxxxx@xxxxxxxx.xxx", "pass", "SMTP")
    smtp.StartTLSMode = True
    T1.Initialize("sender",1*DateTime.TicksPerMinute)
    T1.Enabled=True
End Sub

sub sender_Tick
    if newMail = true and busy = false then
        newMail = false
        wait for (getMAilList) Complete (mailList as list) 'get mailList from DB
        sendMailList(mailList)
    End if
End Sub

Sub sendMailList (correos as list)
    busy = true
    DateTime.DateFormat= "EEE, dd MMM yyyy HH:mm:ss Z"
    Dim date As String = DateTime.Date(DateTime.Now)
    Log(date)
    Log(correos)
    
    Dim mensaje As String = "message"
    Dim email As String
    For x=0 To correos.Size-1   
        Sleep(5000)
        email=correos.Get(x)
        email=email.Trim
        Try
            smtp.To.Add(email)
            Dim MAPHEADER As Map
            MAPHEADER.Initialize
            MAPHEADER.Put("Content-Transfer-Encoding", "7bit")' ESTE HEADER PERMITE QUE GMAIL LEA LOS ARCHIVOS HTML SIN PROBLEMAS
            MAPHEADER.Put("Date", date)
            smtp.AdditionalHeaders = MAPHEADER
            smtp.Sender="Name <xxxxx@xxxxxxxxxx.xxx>"
            smtp.Subject = "subject"
            smtp.HtmlBody=True
            
            smtp.Body = mensaje
            wait for (smtp.Send) SMTP_MessageSent (Success As Boolean)
            Log(Success)
            If Success Then
                
                Log("EMAIL Message sent successfully")
                
            Else
                Log("Error sending EMAIL message")
                Log(LastException.Message)
            End If
        Catch
            Log("ERROR:")
            Log(LastException.Message)
        End Try
    Next
    busy = false
End Sub

I know that another way to do this could be to initialize the smtp connection every time I send an email, but by doing that, you could get an error of too many concurrent connections (421 Too many concurrent SMTP connections; please try again later.)

thanks in advance
 

teddybear

Well-Known Member
Licensed User
I know that another way to do this could be to initialize the smtp connection every time I send an email, but by doing that, you could get an error of too many concurrent connections (421 Too many concurrent SMTP connections; please try again later.)

thanks in advance
You don't need to initialize the smtp connection every time, just when sending error(Success is False), you do initialize it once more
 
Upvote 0
Top