B4J Question Using SMTP - continually fails

Mark Stuart

Well-Known Member
Licensed User
Longtime User
It's disappointing that I can't get the SMTP to send email thru Outlook.
I've tried many settings and always get back this error:
(SMTPConnectionClosedException) org.apache.commons.net.smtp.SMTPConnectionClosedException: Connection closed without indication.

Many users have posted their methods to the forum, which I've tried, but many of these users are unable to send email - mostly using other providers.
I eventually found a post that Erel replied to where the poster was wanting to find and read SMTP documentation. Erel posted a screen shot of the SMTP documentation, instead of posting the URL to the documentation.
I applied what the documentation said to my code, still didn't work.
This is my last code try and it gets the above error.

Has this jNet library and the SMTP methods even been tested lately by Anywhere Software? Seems not as the basic workings do not work.

If the jNet library is not going to work, time to move on to a host like MailGun, which I'm setting up now. Complicated to setup but will work I'm sure.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private smtp As SMTP
    Private smtpServer As String = "smtp.office365.com"
    Private smtpPort As Int = 587
    Private smtpUser As String = "[email protected]"
    Private smtpPassword As String = "xxxxxxxxxxxxxx"
    Private Recipient As String = "[email protected]"
End Sub

Public Sub Send(ToAddress As String, Subject As String, Body As String) As ResumableSub
    smtp.Initialize(smtpServer, smtpPort, smtpUser, smtpPassword, "SMTP")
    Log("Initialized")
    
    smtp.StartTLSMode = True
    smtp.UseSSL = False
    smtp.AuthMethod = smtp.AUTH_LOGIN
    Log("Connection methods set")
    
    smtp.Sender = smtpUser
    smtp.To.Add(Recipient)
    
    smtp.Subject = Subject
    smtp.HtmlBody = True
    smtp.Body = Body
    
    Log("Email Body set")
    
    'Dim trustall As CustomTrustManager
    'trustall.InitializeAcceptAll
    'smtp.SetCustomSSLTrustManager(trustall)
    'smtp.AdditionalHeaders.Put("Content-Type", "text/html; charset=utf-8")
    
    Wait For (smtp.Send) SMTP_MessageSent (Success As Boolean)
    
    If Success Then
        Log("Email sent: " & ToAddress)
    Else
        Log("Email FAILED:")
        Log(LastException)
    End If
    
    Return Success
End Sub
 

Chris2

Active Member
Licensed User
Longtime User
Just a guess, but are you using the latest jNet version (1.83)?
According to the post below there was an SMTP fix in this version and I don't think it has yet been in a B4J release. Maybe it will help with your issue:
 
Last edited:
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Also see:

And

These seem to highlight Outlook specific issues.
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
That is a B4A library. In the zip file are Net.jar and Net.xml, not jNet.
Read @Erel's post that I linked to:
V1.83 - Fixes an issue with SMTP and IPv6 clients.

Tip: B4A Net and B4J jNet libraries are identical. v1.83 will be included in next update of B4J (it is an internal library). You can also use the library attached to first post with B4J.
I guess you can just rename the files to jNet.
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
I renamed the files to jNet and tried my app again. Same error.
Like I said, I'm wondering if these have been tested by more than one person. :(
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
I should also mention that my email provider will not enable SMTP AUTH. Therefore this is most likely the problem I'm having, where my email will not go thru and the reason for the error.

If that is the case, how is this jNet library to work with the SMTP method and the email provider not giving SMTP AUTH access?
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
I should also mention that my email provider will not enable SMTP AUTH. Therefore this is most likely the problem I'm having, where my email will not go thru and the reason for the error.

If that is the case, how is this jNet library to work with the SMTP method and the email provider not giving SMTP AUTH access?
If I remember correctly, Outlook stopped supporting SMTP in 2025 and switched to sending emails using the API of Azure Communication Service.
If the email provider does not support SMTP AUTH, the jnet library will not work, unless you change your email service provider
 
Upvote 0

Mark Stuart

Well-Known Member
Licensed User
Longtime User
Thanx for the update teddybear.
Unfortunately for Erel, that's the path I'm having to take.
I'm going with MailGun. The setup is complicated, having to do lots of it - which is a good thing I suppose, as they don't need their customers spamming everyone in the world ;)

Mark
 
Upvote 0
Top