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.basic4ppc.com/android/fo...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.basic4ppc.com/android/fo...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, "xxx@gmail.com", "yyy", "SmtpClient")
      smtpClient.StartTLSMode = True
   End If
End Sub
 

Attachments

  • Net.zip
    189.1 KB · Views: 882
Last edited:

xky

Member
Licensed User
I use it and it work fine.
But if I upload a file, if the file name is a UTF8 charactor, it will have 553 error.
For example: 1.txt is uploaded OK; 你好.txt will be have 553 (could not create file) back.
 

Erel

Administrator
Staff member
Licensed User
I was able to upload a file with that name to an FTP server. Though it seems like its name was later lost. The control channel uses ISO-8859-1 encoding.

You can change it with this code:
B4X:
Dim r As Reflector
r.SetStaticField("org.apache.commons.net.ftp.FTP", "DEFAULT_CONTROL_ENCODING", "UTF-8", "java.lang.String")
It didn't make any difference with the server I've tested.

Interesting reading about this issue: https://wiki.filezilla-project.org/Character_Encoding
 

xky

Member
Licensed User
upload_2016-10-7_11-9-10.png

I use a Linksys EA6700 Router, it has FTP, I tested each encode, but it still have the 553 problem. I added the Reflector as you given the code to me, it's not work.
But I can drag a file name like 你好.txt to the FTP server with windows file manager. So I think there's something I can do but I don't know.
B4X:
Sub Button3_Click
    CardList=File.ListFiles(File.DirDefaultExternal)
    If CardList.Size=0 Then Return
    Dim r As Reflector
    r.SetStaticField("org.apache.commons.net.ftp.FTP", "DEFAULT_CONTROL_ENCODING", "UTF-8", "java.lang.String")
    FTP.Initialize("FTP", "server.lingker.com", 21, "ShowMe", "00000000")
    Dim i As Byte
    Dim s As String
    For i = 0 To CardList.Size-1
        s=CardList.Get(i)
        FTP.UploadFile(File.DirDefaultExternal, s, True, "/ShowMe/" & s)
    Next
    FTP.Close
End Sub
This is my code, you can use the infomation in code to connect my FTP for Test.
I use CuteFTP to upload 测试这个文件.txt is OK. So I think B4A can do it too.
FTP express pro is an android app, it can upload to my server with 测试这个文件.txt too. Why this lib can't?
 
Last edited:

xky

Member
Licensed User
I was able to upload a file with that name to an FTP server. Though it seems like its name was later lost. The control channel uses ISO-8859-1 encoding.

You can change it with this code:
B4X:
Dim r As Reflector
r.SetStaticField("org.apache.commons.net.ftp.FTP", "DEFAULT_CONTROL_ENCODING", "UTF-8", "java.lang.String")
It didn't make any difference with the server I've tested.

Interesting reading about this issue: https://wiki.filezilla-project.org/Character_Encoding
It seems it do nothing, still not UTF-8.
 

xky

Member
Licensed User
Yeah,I'm very happy to tell you. It's worked. I can upload any name of file now.(such as 测试这个文件.txt)
So I think it is different.
B4X:
r.SetField("_controlEncoding", "UTF8", "java.lang.String")
change current encoding to UTF-8 but
B4X:
r.SetStaticField("org.apache.commons.net.ftp.FTP", "DEFAULT_CONTROL_ENCODING", "UTF-8", "java.lang.String")
not.
I suggest add FTP.SetControlEncoding to the lib, for non-ASCII users.

Thankyou very much for the helping.
 

MMORETTI964

Member
Licensed User
I've a problem downloading (or uploading) a large file with FTP libraries.
Should be something related to a firewall who closes the command connection.

There are something I can use for keeping alive the connection (a keepalive socket option or a "NOOP" command - not-recommended but sometime useful - while transferring a file)?

Thank you.

Maurizio
 

MarcoRome

Expert
Licensed User
I've a problem downloading (or uploading) a large file with FTP libraries.
Should be something related to a firewall who closes the command connection.

There are something I can use for keeping alive the connection (a keepalive socket option or a "NOOP" command - not-recommended but sometime useful - while transferring a file)?

Thank you.

Maurizio
Do you see THIS
 

MMORETTI964

Member
Licensed User
I can't send a command on the same connection while a command is executing...
I'm trying to change the KeepAlive on that server.
I think if I could set KeepAlive on the command control (or send a NOOP on that command) I could bypass every firewall problem I would encounter on my way...

Maurizio
 

MarcoRome

Expert
Licensed User
Hi @Erel.
Can i set a time-out in the current library type:
ftpClient.setControlKeepAliveTimeout(xxx)
or to use this command to 'keep-alive' ??
Thank you
Marco
 

JackKirk

Well-Known Member
Licensed User

aviario

Active Member
Licensed User
Hi. first of all, sorry for my english.
We have a program in B4A 6.5 versión using this library to connect to Fillezilla FTP Server.
Now we change to B4A 7.3 versión and the software not run. When we try to connect, the program not do it.
There an update of library?

tha
 

Steini1980

Member
Licensed User
Hi@all,
after upgrading the NET-Library to newest Version 1.77 i got the following exception if I try to start my App:

B4X:
Installing file.
PackageAdded: package:b4a.dms.blabla
java.lang.ClassNotFoundException: anywheresoftware$b4a$net$FTPWrapper
    at anywheresoftware.b4a.shell.Shell.getCorrectClassName(Shell.java:544)
    at anywheresoftware.b4a.shell.Shell.createObject(Shell.java:516)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:320)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at b4a.dms.blabla.main.afterFirstLayout(main.java:91)
    at b4a.dms.blabla.main.access$100(main.java:16)
    at b4a.dms.blabla.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6873)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Could you please help me?! does I have to delete anything before?
 
Status
Not open for further replies.
Top