B4A Library New Net library - Android FTP, SMTP and POP3

Status
Not open for further replies.
The Net library supports FTP, SMTP and POP3 protocols. This library replaces the FTP library. Both regular connections and SSL connections are supported.
SMTP - Allows to directly connect to SMTP mail servers and send mails, including Html messages and attachments.
POP3 - Allows to directly connect to POP3 mail servers and download messages. Currently the messages are not parsed. The raw string is returned. You can use MailParser class to parse the raw messages.

Installation instructions:
- Download the attach file.
- Copy Net.xml and Net.jar to the additional libraries folder. Make sure that there are no older copies in the internal libraries folder.

V1.81 - latest version
V1.80 - SMTP, POP and FTP can be configured to use a custom trust manager. This allows accepting invalid certificates.

V1.77 - New Sender.MailFrom field. Allows setting the mail address that is sent with the MAIL command. By default it is the same as the Username field.

V1.75 - Adds a configurable timeout parameter - FTP.TimeoutMs. Default timeout is set to 60000 (60 seconds).

V1.70 - Adds support for calling with Wait For: https://www.b4x.com/android/forum/threads/b4x-net-library-ftp-smtp-pop-with-wait-for.84821/
SMTP.AdditionalHeaders map. Allows adding headers to the messages.

V1.63 - Fixes an issue with SMTP mails with attachments. The closing boundary was previously missing.
V1.62 - Fixes an issue with SMTP in StartTLS mode.
V1.61 - Fixes an issue in SMTP related to the content encoding not being set in multipart messages.

V1.60 - New method: FTP.AppendFile. Similar to UploadFile. Appends the data to an existing file if such exists. It sends the APPE FTP command.

V1.53 - Fixes an issue with FTP.CloseNow and SSL connections.

V1.52 - Adds support for different types of authentication methods (SMTP): http://www.b4x.com/android/forum/th...d-ftp-smtp-and-pop3.10892/page-11#post-232432

V1.51 is released. Fixes an issue with FTP over SSL explicit mode.

V1.50 is released. See this link: http://www.b4x.com/android/forum/th...d-ftp-smtp-and-pop3.10892/page-10#post-231145

V1.37 is released. This version removes the automatic escaping of '=' characters in SMTP messages. To implement it in your code you should write:
B4X:
SMTP.Body = SMTP.Body.Replace("=", "=3D")


V1.35 is released. This version adds support for STARTTLS mode.
Unlike UseSSL mode the connection is "upgraded" to a secured connection only after the client sends the STARTTLS command.
Most of the popular smtp servers support this mode, usually on port 587.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      smtpClient.Initialize("smtp.gmail.com", 587, "[email protected]", "yyy", "SmtpClient")
      smtpClient.StartTLSMode = True
   End If
End Sub
 

Attachments

  • Net.zip
    189.1 KB · Views: 3,003
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
You can encrypt the password.

I'm having trouble with the lib on my gmail account. POP refuses to initialize, but the other one does just fine
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
JAVA.LANG.RUNTIMEEXCEPTION: POP3 SHOULD FIRST BE INITIALIZED, even though I have initialized it

One containing the text "Error listing messages"
But it sends email just fine.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Yes, but when I get the error, I forcefully reinitialize it, and I still get that error.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I think I figured it out. I was being too aggressive with the CheckEmail routine.
 

Mahares

Expert
Licensed User
Longtime User
According to Erel, MailParser doesn't support all possible formats. Has anybody been able to get it to parse from email clients other than Gmail, particularly Outlook. If so, can you provide some tips or help.
Thanks
 

aggelos

Member
Licensed User
Longtime User
Hello,

I am using b4a 3.82 on nexus s 4.1.2

i can upload and download files but i cannot use the ftp.sendcommand

I have this code
B4X:
Dim FTP1 As FTP

FTP1.Initialize("FTP1",txtFTPserverhost.Text,txtFTPserverport.Text,txtFTPserveruser.Text,txtFTPserverpass.text)
FTP1.SendCommand("MKD" , "/ftptest")


when i press ALT+3 i get this message from b4a

B4X:
---------------------------
Basic4android
---------------------------
An error occurred.
B4A line: 459
FTP1.SendCommand(\
javac 1.7.0_25
src\eu\sitediary\client\showftpsync.java:442: error: cannot find symbol
mostCurrent._ftp1.SendCommand(processBA,"MKD","/ftptest");
  ^
  symbol:  method SendCommand(BA,String,String)
  location: variable _ftp1 of type FTPWrapper
---------------------------
ΟΚ   
---------------------------

help? :)
 

aggelos

Member
Licensed User
Longtime User
1.53 .downloaded yesterday

Am i missing a library dependancy?
I also downloaded and installed java 1.8. same error message

And i tried using an empty project with only core 3.80 and net 1.53 libs selected. same error
 

aggelos

Member
Licensed User
Longtime User
Thank you, there was a net.jar in the internal libraries folder from the old days 2011.
as soon i removed it everything runs ok.
 

aggelos

Member
Licensed User
Longtime User
If you need utf-8 support with your FTP use this code exactly after FTP1.initialize.....
Tested with English and Greek characters on Filezilla FTP server 0.9.48

B4X:
Dim r As Reflector
r.Target=FTP1
r.Target = r.GetField("client")
'Log("current encoding " & r.RunMethod("getControlEncoding"))
r.RunMethod2("setControlEncoding","UTF-8","java.lang.String")

I also use the following command but this depends on the FTP Server and most modern servers are already using UTF8
So you may not need this line.
FTP1.SendCommand("OPTS UTF-8 ON","")
 

touchsquid

Active Member
Licensed User
Longtime User
I have been trying to use the NET library 1.53 to upload files to my server. I can't even get a directory listing. It works fine with a browser or FTP app on my PC.

I tried with the following test web site which does not need credentials:

ftp://ftp.kernel.org/

The error returned in all cases is success = false and this error:
libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)

I am using a real Android 4.4.2 device, not the emulator. Any ideas would be appreciated.
Dim MyFTP as FTP
MyFTP.Initialize("FTP1", "ftp://ftp.kernel.org/", 21 , "", "")
MyFTP.List("/")
 

touchsquid

Active Member
Licensed User
Longtime User
I tried that too, still the same result. However I gave up on FTP and used an ASMX web service instead and it works fine.
 

rebuz

Member
Licensed User
Longtime User
I get an error by sending a Mail over SMTP. can i change the trustet level like:

cert.InitializeAcceptAll for smtp?
SMTP error.png

regards, Remo
 

rebuz

Member
Licensed User
Longtime User
Hi Erel,
Thx for watch my problem, her the Log

With StartTLS setting

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:477)

at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:328)

at org.apache.commons.net.smtp.AuthenticatingSMTPClient.performSSLNegotiation(AuthenticatingSMTPClient.java:285)

at org.apache.commons.net.smtp.AuthenticatingSMTPClient.execTLS(AuthenticatingSMTPClient.java:265)

at anywheresoftware.b4a.net.SMTPWrapper$1.run(SMTPWrapper.java:243)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:444)

at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

at java.util.concurrent.FutureTask.run(FutureTask.java:138)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

at java.lang.Thread.run(Thread.java:1019)



Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:161)

at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:664)

at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)

at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:474)

... 10 more

Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

at org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:207)

at java.security.cert.CertPathValidator.validate(CertPathValidator.java:197)

at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:156)

... 13 more


White SSL Setting

javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xa372d00: Failure in SSL library, usually a protocol error

error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:683 0x80a40440:0x00000000)

at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)

at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:474)

at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:750)

at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:692)

at org.apache.commons.net.SocketClient._connectAction_(SocketClient.java:146)

at org.apache.commons.net.smtp.SMTP._connectAction_(SMTP.java:257)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:173)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:193)

at anywheresoftware.b4a.net.SMTPWrapper$1.run(SMTPWrapper.java:231)



at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:444)

at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

at java.util.concurrent.FutureTask.run(FutureTask.java:138)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

at java.lang.Thread.run(Thread.java:1019)

thx
regards Remo
 
Status
Not open for further replies.
Top