B4J Question SMTP email classification

BlueVision

Active Member
Licensed User
Longtime User
Unfortunately I am a bit out of my depth when it comes to understanding the communication with outgoing mail servers, maybe someone from the forum can help me.
Sending emails works fine from my private email account with the code I use.
When accessing the SMTP server of the company network, I get the following message, which I unfortunately cannot interpret: (port used is: 587)
B4X:
org.apache.commons.net.smtp.SMTPConnectionClosedException: Connection closed without indication.
    at org.apache.commons.net.smtp.SMTP.__getReply(SMTP.java:199)
    at org.apache.commons.net.smtp.SMTP.__sendCommand(SMTP.java:171)
    at org.apache.commons.net.smtp.SMTP.__sendCommand(SMTP.java:186)
    at org.apache.commons.net.smtp.SMTP.rcpt(SMTP.java:524)
    at org.apache.commons.net.smtp.SMTPClient.addRecipient(SMTPClient.java:314)
    at anywheresoftware.b4a.net.SMTPWrapper$1.run(SMTPWrapper.java:298)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Maybe the problem is that all emails have to be classified (External, Internal, Confidential) before being sent by the company's SMTP server?
Maybe someone can interpret the codes in the log correctly or tell me where to find an explanation? I can only explain the error by the missing classification of the email or maybe there are other security measures that block the sending of emails via direct access to the SMTP server.
The company uses Outlook365:
B4X:
SMTP.Initialize("smtp.office365.com",587,Email.Text,Password.Text,"SMTP")
Should it be due to the missing classification, how can a mail to be sent via SMTP already be classified in the code?
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
How have you set the encryption? Office365 requires STARTTLS

See also this.
 
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
It is set to true.
This is the routine for initiating the SMTP transaction. Works very fast and reliable when replacing the SMTP-server with a different one. Took this snippet from a post by Erel. So there must be another reason for failing when trying to access the outlook-SMTP-server.
Just checked the SMTP-settings for outlook356. It is:
Server: smtp.office365.com
Port: 587
Encryption: STARTTLS

B4X:
    Public SMTP As SMTP
    SMTP.Initialize("smtp.office365.com",587,TFEmailCE.Text,TFPassword.Text,"SMTP")
    SMTP.Subject = Subject
    SMTP.Body = Body
    SMTP.To.Add(MailTo1)
    SMTP.To.Add(MailTo2)
    SMTP.StartTLSMode = True
    SMTP.Send
    Wait For SMTP_MessageSent(Success As Boolean)
    Log(Success)
    If Success Then
        Log("Message sent successfully")
    Else
        Log("Error sending message")
        Log(LastException.Message)
    End If
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
full disclosure: i don't use outlook.
but a little research refers to what microsoft calls an SMTP AUTH client.
"As long as your scenario meets the requirements for SMTP AUTH client submission,"
you can send email with the settings you described. you need to find out how to
obtain an SMTP AUTH client. it appears to identify you as an authorized user of the
service. this would have nothing to do with b4x, since microsoft's documentation
indicates that normal stmp flow would follow, IF you have an outlook's so-called
SMTP AUTH client in place. it could be trivial to configure.

as to the error messages, what it boiled down to in the end was you didn't receive
a reply from the server to a command which you sent. as a result, the server
timed out the connection.

the smtp dialog between client and server is very, very simple and unforgiving.
just google "smtp dialog" for examples. the dialog consists of a handful of exchanges
to complete the handshake. miss one and you're out. if you were adept at, eg, telnet,
you could carry out such a dialog from a terminal emulator and your keyboard to see
exactly what happened. unfortunately, with a library, unless it has a "verbose" setting,
the dialog takes place unseen, and if there's an error, you see an error message,
whose contents depend on the good will of the programmers who wrote the server
and the client code as to exactly what is revealed. in this case, almost nothing. i do see
that you send 2 commands in a row without a reply in between. not a good sign.

also, just so you know, lines 7 - 12 in the log occurred first. the network transactions
run on a different thread. those lines pertain to that. lines 1-6, actually 6 - 1, are the
smtp dialog between you and the server, ending with the server bailing out.
 
Last edited:
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
@BlueVision I don't understand the underlying security, but it could be due to the security policies of your work email server because in my case, my work only allows Outlook as a client on my mobile phone to access 365 mail and will not accept the inbuilt Samsung mail client. Others with more knowledge may be able to elaborate if this is relevant.
 
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
Thank you very much for the explanations. My assumptions go in a similar direction. Since the code works without any problems with another SMTP server, it is obvious that the IT administration may have installed other hurdles that make simple authentication via user/password even more difficult for security reasons. I will contact the IT administration to clarify the matter.
A workaround for the problem could be the use of another mail server, which then generates an "external" email and sends it to the company network. This definitely works, but has the disadvantage that an appropriate warning is then integrated into the email.
But even this would not be a problem, as it is a pure text email (no links, no attachments) in which it would be difficult to hide malware. In other words, this email to the same group of people with the same sender can quickly be classified as harmless by the recipient, as long as it is not classified as spam by the email filters of the company's internal IT. I have already tried this, it works this way.

Thanks to everybody for responding, BV
 
Upvote 0
Top