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

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:
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.

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


  • Net.zip
    189.1 KB · Views: 1,624
Last edited:


Licensed User
Longtime User
Wow Erel! Many thanks for this. :icon_clap:

I am in the office now so I cannot test it. I noted that SMTP permits attachments and there is also SSL-support. Wonderful! :sign0060:

As to POP3, when you say
Currently the messages are not parsed
are you intending attachments and MIME-handling?


B4X founder
Staff member
Licensed User
Longtime User
are you intending attachments and MIME-handling?
I actually tried several solutions before releasing this library, however none of them worked properly. The case with POP3 is that the general problem of supporting all the possible encodings and messages is very complicated.
However using POP3 to receive messages with a known format is much simpler.

If there is interest I can write a module that will help with saving attachments and with the headers.

As I wrote in the first post I think that the POP3 is a very important feature as it is a reliable method to send information to devices.


Well-Known Member
Licensed User
Longtime User
Just the library I was looking for. SMTP library makes it really easy to send emails, that too with attachments.

One request. Is it possible to add IMAP to this library? IMAP enables the user to download certain folders (or labels) instead of downloading the whole thing. Implementing an attachment download feature might be possible as well. Google has a documentation page on IMAP extensions for Gmail, but none of that makes sense to me.


Licensed User
Longtime User
never receive any messages

Hi Erel,

I'll try to get the .net lib working. I do not get any errors but in the ListCompleted() event the Messages is always empty although there are mails in my inbox.

I've activated the pop download in GMail (see images.zip).

In my Desktop app (RealBasic) I get the mail from the inbox.

'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim pop As POP3
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If firstTime Then
      POP.Initialize("pop.gmail.com", 995, "my@gmail.com", "mypwd", "pop")
      POP.UseSSL = True 'Gmail requires SSL.
   End If
End Sub

Sub pop_ListCompleted (Success As Boolean, Messages As Map)
        Log ("Success: " & success)
   If Success = False Then
      'download all messages
      'change last parameter to True if you want to delete the messages from the server
      Log ("messages: " & Messages.Size)
      For i = 0 To Messages.Size - 1
         pop.DownloadMessage(Messages.GetKeyAt(i), False)
   End If
   pop.CloseEnd Sub

Sub pop_DownloadCompleted (Success As Boolean, MessageId As Int, MessageText As String)
   If Success = False Then
      'Parse the mail
      Dim m As Message
      Dim s As String
      s = MailParser.GetSubject(MessageText)
      If s.Contains("P000001") AND s.Contains("[OUT]") Then
         m = MailParser.ParseMail(MessageText, File.DirRootExternal)
      End If
   End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

The result in the log:

ID[0]=0(0) Dn(0=>1)
Dsptch > Window{48602558 com.android.packageinstaller/com.android.packageinstaller.InstallAppProgress paused=false}
Dsptch > Window{48602558 com.android.packageinstaller/com.android.packageinstaller.InstallAppProgress paused=false}
ID[0]=0(0) Up(1=>0)
Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.AB.OneTwoANWork/.main }
Start proc com.AB.OneTwoANWork for activity com.AB.OneTwoANWork/.main: pid=8075 uid=10168 gids={1015, 3003}
Zygote: pid 8075 has INTERNET permission, then set capability for CAP_NET_RAW
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Using factory org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl@4006ef08
Displayed activity com.AB.OneTwoANWork/.main: 1047 ms (total 1047 ms)
Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.
[B]Success: true[/B]
[B]messages: 0[/B]
Timer 0x3->0x3|0x0
GC_EXPLICIT freed 272 objects / 17424 bytes in 136ms


  • images.zip
    67.8 KB · Views: 745


Licensed User
Longtime User
problem compile .net library

[ompiling code. 0.14
Generating R file. 0.00
Compiling generated Java code. 2.17
Convert byte code - optimized dex. Error
java.lang.IllegalArgumentException: already added: Lorg/apache/commons/net/DatagramSocketClient;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:143)
at com.android.dx.command.dexer.Main.processClass(Main.java:372)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:346)
at com.android.dx.command.dexer.Main.access$400(Main.java:59)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:294)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:244)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:130)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:108)
at com.android.dx.command.dexer.Main.processOne(Main.java:313)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:233)
at com.android.dx.command.dexer.Main.run(Main.java:185)
at com.android.dx.command.dexer.Main.main(Main.java:166)
at com.android.dx.command.Main.main(Main.java:90)
1 error; aborting
Standard dexer.


Licensed User
Longtime User
I found the solution .., thank you very much

I found the solution .., thank you very much

The problem was that I had the library, ABFTP, remove them, and make again for communications with the new library. net and it works perfect.

Many thanks, Erel, are excellent administrator.

google traductor.
only spanish.

Hubert Brandel

Active Member
Licensed User
Longtime User

this LIB is called NET.* in the library dir I have a NETWORK.* do we have to rename the file, or is it different ?

OK, I just see NET -> SMTP, NETWORK -> Sockets, so I will need both ;-)
Last edited:
Not open for further replies.