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

Discussion in 'Additional libraries, classes and official updates' started by Erel, Aug 17, 2011.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    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.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.StartTLSMode = 
    End If
    End Sub

    Attached Files:

    • Net.zip
      File size:
      186.5 KB
    Last edited: Oct 4, 2018
    ilan, Watchkido1, welu1805 and 14 others like this.
  2. ZJP

    ZJP Active Member Licensed User

    Yessssssss !!!!!!! :sign0060:

  3. moster67

    moster67 Expert Licensed 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
    are you intending attachments and MIME-handling?
  4. Kiffi

    Kiffi Well-Known Member Licensed User

    Thanks a lot, Erel!


    Greetings ... Kiffi
  5. Brad

    Brad Active Member Licensed User

    you rock man!:sign0060:
  6. Erel

    Erel Administrator Staff Member Licensed User

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

    Erel Administrator Staff Member Licensed User

  8. dealsmonkey

    dealsmonkey Active Member Licensed User

    Kwame Twum likes this.
  9. peacemaker

    peacemaker Well-Known Member Licensed User

    Good news !
    Erel, is it hard to add NNTP protocol ? :)
  10. Inman

    Inman Well-Known Member Licensed 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.
  11. alwaysbusy

    alwaysbusy Expert Licensed 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.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
    End If

    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, 
    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

    Attached Files:

    dost likes this.
  12. Erel

    Erel Administrator Staff Member Licensed User

    I've just tried similar code with my Gmail account and it worked properly. Try sending a new message to your account and then run the Android program.
  13. fargoz

    fargoz Member Licensed 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.
  14. Erel

    Erel Administrator Staff Member Licensed User

    Which libraries did you reference?
    Can you upload your project?
  15. fargoz

    fargoz Member Licensed 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.
    Maki25 likes this.
  16. dfsantos

    dfsantos Member Licensed User

    I am also interesed
    it is possible??
  17. Erel

    Erel Administrator Staff Member Licensed User

    I plan to add it in the future.
  18. dfsantos

    dfsantos Member Licensed User

    Thanks for you work :sign0060::sign0060::sign0060:
  19. Hubert Brandel

    Hubert Brandel Active Member Licensed 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: Oct 9, 2011
  20. Erel

    Erel Administrator Staff Member Licensed User

    As you saw these are two different libraries. Network is for TCP / UDP communication and Net library is an implementation of several protocols: FTP, SMTP and POP3.
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice