B4J Question SMTP send mail issue from outlook

teddybear

Well-Known Member
Licensed User
I configured IMAP and SMTP in a third-party mail client as required by outlook,It works well.
In B4j I use same way to set SMTP, it does not work, my code is
B4X:
    SMTP.Initialize("mailserver", portnumber ,"[email protected]" , "password", "smtp")
    SMTP.Subject = Subject
    SMTP.Body = Body
    SMTP.To.Add(MailTo)
    SMTP.StartTLSMode = True
    SMTP.Send
error is
B4X:
Program started.
SMTP Initialize Done
SendEmail
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.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
eMail Failed to send - To: [email protected] Subject: subject test1
org.apache.commons.net.smtp.SMTPConnectionClosedException: Connection closed without indication.

I searched for the issue on the forums and found 2 threads(thread1 and thread2) that same issue, Both of them seem to have solved the issue. I downloaded the example (v0.4) of thread1 in post #33 and it does not work yet.
 
Solution
Perhaps this could help you, this is working for me:

View attachment 127829
SMTP Initialization:
    SMTP.Initialize("smtp.outlook.com", 587 ,"[email protected]" , "password", "Mailer")
 
    Dim M As Map
    M.Initialize
    M.Put("Content-Type", "text/html; charset=utf-8")
    M.Put("Content-Transfer-Encoding", "7bit")
    smtp.AdditionalHeaders = M
  
    smtp.StartTLSMode = True
    smtp.UseSSL = True
    smtp.Sender = "<" & "[email protected]" & ">
    smtp.To.Add("[email protected]")
    smtp.Subject = $"Test $date{DateTime.Now} - $time{DateTime.Now}"$
 
     smtp.HtmlBody = True
'    smtp.Body = "This is the body text of the email"
'    smtp.Body = File.ReadString(File.DirApp,"test1b.html")
    smtp.Body = CreateHtmlBody
  
    Dim trustall As...

MicroDrie

Well-Known Member
Licensed User
Perhaps this could help you, this is working for me:

1649711977654.png

SMTP Initialization:
    SMTP.Initialize("smtp.outlook.com", 587 ,"[email protected]" , "password", "Mailer")
   
    Dim M As Map
    M.Initialize
    M.Put("Content-Type", "text/html; charset=utf-8")
    M.Put("Content-Transfer-Encoding", "7bit")
    smtp.AdditionalHeaders = M
    
    smtp.StartTLSMode = True
    smtp.UseSSL = True
    smtp.Sender = "<" & "[email protected]" & ">"
    smtp.To.Add("[email protected]")
    smtp.Subject = $"Test $date{DateTime.Now} - $time{DateTime.Now}"$
   
     smtp.HtmlBody = True
'    smtp.Body = "This is the body text of the email"
'    smtp.Body = File.ReadString(File.DirApp,"test1b.html")
    smtp.Body = CreateHtmlBody
    
    Dim trustall As CustomTrustManager
    trustall.InitializeAcceptAll
    smtp.SetCustomSSLTrustManager(trustall)
    smtp.AuthMethod = smtp.AUTH_LOGIN
    
    smtp.Send

Create HTML body of teh email:
Sub CreateHtmlBody() As String
    Dim HTML As StringBuilder
    HTML.Initialize
    ' Your HTML text
    HTML.Append($""$)
    Return HTML
End Sub

It has been quite some time since I made this program code under B4J. If I remember correctly the challenge was the extra mail header and the SSL. The outlook.com port number you should use is 587.
 
Upvote 1

teddybear

Well-Known Member
Licensed User
Perhaps this could help you, this is working for me:

View attachment 127829
SMTP Initialization:
    SMTP.Initialize("smtp.outlook.com", 587 ,"[email protected]" , "password", "Mailer")
 
    Dim M As Map
    M.Initialize
    M.Put("Content-Type", "text/html; charset=utf-8")
    M.Put("Content-Transfer-Encoding", "7bit")
    smtp.AdditionalHeaders = M
  
    smtp.StartTLSMode = True
    smtp.UseSSL = True
    smtp.Sender = "<" & "[email protected]" & ">
    smtp.To.Add("[email protected]")
    smtp.Subject = $"Test $date{DateTime.Now} - $time{DateTime.Now}"$
 
     smtp.HtmlBody = True
'    smtp.Body = "This is the body text of the email"
'    smtp.Body = File.ReadString(File.DirApp,"test1b.html")
    smtp.Body = CreateHtmlBody
  
    Dim trustall As CustomTrustManager
    trustall.InitializeAcceptAll
    smtp.SetCustomSSLTrustManager(trustall)
    smtp.AuthMethod = smtp.AUTH_LOGIN
  
    smtp.Send

Create HTML body of teh email:
Sub CreateHtmlBody() As String
    Dim HTML As StringBuilder
    HTML.Initialize
    ' Your HTML text
    HTML.Append($""$)
    Return HTML
End Sub

It has been quite some time since I made this program code under B4J. If I remember correctly the challenge was the extra mail header and the SSL. The outlook.com port number you should use is 587.

Perhaps this could help you, this is working for me:

View attachment 127829
SMTP Initialization:
    SMTP.Initialize("smtp.outlook.com", 587 ,"[email protected]" , "password", "Mailer")
  
    Dim M As Map
    M.Initialize
    M.Put("Content-Type", "text/html; charset=utf-8")
    M.Put("Content-Transfer-Encoding", "7bit")
    smtp.AdditionalHeaders = M
   
    smtp.StartTLSMode = True
    smtp.UseSSL = True
    smtp.Sender = "<" & "[email protected]" & ">"
    smtp.To.Add("[email protected]")
    smtp.Subject = $"Test $date{DateTime.Now} - $time{DateTime.Now}"$
  
     smtp.HtmlBody = True
'    smtp.Body = "This is the body text of the email"
'    smtp.Body = File.ReadString(File.DirApp,"test1b.html")
    smtp.Body = CreateHtmlBody
   
    Dim trustall As CustomTrustManager
    trustall.InitializeAcceptAll
    smtp.SetCustomSSLTrustManager(trustall)
    smtp.AuthMethod = smtp.AUTH_LOGIN
   
    smtp.Send

Create HTML body of teh email:
Sub CreateHtmlBody() As String
    Dim HTML As StringBuilder
    HTML.Initialize
    ' Your HTML text
    HTML.Append($""$)
    Return HTML
End Sub

It has been quite some time since I made this program code under B4J. If I remember correctly the challenge was the extra mail header and the SSL. The outlook.com port number you should use is 587.
Thanks a lot for your help. Your code works well
 
Upvote 0
Solution
Top