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:

biometrics

Active Member
Licensed User
Longtime User
I see it now. The REST command is not sent by default. You will need to send it using SendCommand.

Hi Erel,

I finally had a chance to test this. You are sending a "TYPE I" before sending the "RETR" command. The RFC states there may be no command between the "REST" and the "RETR". By sending the "TYPE I" it's cancelling the "REST". Could you remove the "AsciiFile As Boolean" parameter and make it a separate function. Or could you do a "TYPE I" when you connect and log in as the default and only change it if "AsciiFile" is true. Surely most people transfer as binary anyway, after all ascii files can be transferred as binary.

Without resume we can't complete out project. We use 3G and we often get disconnects and we transfer large media files. The way it is now it will always start from the beginning.

Hoping to hear positive news from you soon.

RFC: RFC 3659 - Extensions to FTP

The server-PI will respond to the REST command with a 350 reply, indicating that the REST parameter has been saved, and that another command, which should be either RETR or STOR, should then follow to complete the restart.

My FTP server log:

B4X:
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "REST 711,200"
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "350 Restart position accepted (0)."
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "TYPE I"
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "200 Switching to Binary mode."
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "PORT 41,185,xxx,xxx,148,125"
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "200 PORT command successful. Consider using PASV."
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "RETR /1000004.swf"
Wed Mar 28 12:03:47 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "150 Opening BINARY mode data connection for /1000004.swf (6807317 bytes)."
 
Last edited:

biometrics

Active Member
Licensed User
Longtime User
Thanks Erel !

I'll test it tomorrow.

Is this a special version or will it become the the standard in your next version?
 

biometrics

Active Member
Licensed User
Longtime User
Hi Erel,

I tested it now ...

You seem to be converting carriage return + line feeds to only line feeds.

E.g. (in hex) 0D 0A has changed to 0A.

I checked the server log, the complete file was served but the resulting file on the device was smaller and I can see in a hex editor that the 0D 0A -> 0A replacement took place.

I haven't tried the resume yet because of this but I do note in the log that you're not sending the "TYPE I" anymore.

I haven't changed my code, I only replaced the Net.jar library.

------------------------------------------------------------------------

Every FTP client, server or developer component I've used supports resume, it's pretty standard. I don't mind activating it in our code.

I'm a little bit worried that if you make me a special version that I won't be able to use updates in the future.

I appreciate that you can't change the function parameters or behaviour as other developers are already using it but perhaps it would be better if you could add another function identical to DownloadFile but that does not have the AsciiFile parameter (I can set it via SendCommand if need be) and does not issue "TYPE I".

------------------------------------------------------------------------

Thanks for your support, I appreciate the quick response.

Looking forward to your reply.
 

biometrics

Active Member
Licensed User
Longtime User
You are probably transferring in ASCII mode. You should use binary mode instead.

Hi Erel,

I have not changed my code at all. This is the function call:

B4X:
libFTP.DownloadFile(sFtpDirectory & sFileName, False, sPath & "/Download/", sFileName)

And the FTP server is in binary mode, from the FTP server log file:

B4X:
Thu Mar 29 11:28:13 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "RETR /smil_1000013.xml"
Thu Mar 29 11:28:13 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "150 Opening BINARY mode data connection for /smil_1000013.xml (1439 bytes)."
Thu Mar 29 11:28:13 2012 [pid 3] [1000002] OK DOWNLOAD: Client "41.185.xxx.xxx", "/smil_1000013.xml", 1439 bytes, 17.58Kbyte/sec
Thu Mar 29 11:28:13 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "226 Transfer complete."
 

biometrics

Active Member
Licensed User
Longtime User
But you asked me to change :)
The type is no longer set by the library. You should explicitly set it.

Hi Erel,

What I mean is it's the same code as yesterday that downloaded the file correctly but wouldn't resume.

All I did is replace the library, now the files are not downloaded correctly.

I did try to do a "TYPE I" before the "REST" and "RETR". As you can see from the log file the server is sending in binary mode:

"150 Opening BINARY mode data connection for /smil_1000013.xml (1439 bytes)."

The file on the device was 1419 bytes.
 

biometrics

Active Member
Licensed User
Longtime User
I see it now.
There is no way to change this behavior from outside the library.

If you like (and think that it will work) I can make another copy which defaults to binary transfers.

Yes please.
 

biometrics

Active Member
Licensed User
Longtime User
Sunday arrived early...

This version doesn't set the file type and defaults to binary transfers.

Thanks Erel. I only noticed this yesterday as I was waiting for the forum to notify me of new posts, but it didn't this time.

I've tested it now but there is still a command being sent between the REST and RETR thus cancelling the resume point.

Log:
B4X:
Wed Apr  4 11:10:37 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "REST 670,392"
Wed Apr  4 11:10:37 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "350 Restart position accepted (0)."
Wed Apr  4 11:10:37 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "PORT 41,185,124,211,172,106"
Wed Apr  4 11:10:37 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "200 PORT command successful. Consider using PASV."
Wed Apr  4 11:10:37 2012 [pid 3] [1000002] FTP command: Client "41.185.xxx.xxx", "RETR /1000004.swf"
Wed Apr  4 11:10:37 2012 [pid 3] [1000002] FTP response: Client "41.185.xxx.xxx", "150 Opening BINARY mode data connection for /1000004.swf (6807317 bytes)."
 

biometrics

Active Member
Licensed User
Longtime User
I'm afraid that the PORT method is sent from the native FTP library.

Oh no!

As you can see from this it won't work then:

Sets the point at which a file transfer should start; useful for resuming interrupted transfers. For nonstructured files, this is simply a decimal number. This command must immediately precede a data transfer command (RETR or STOR only); i.e. it must come after any PORT or PASV command.

What is the native FTP library you use called? I'd like to read up on it and find out if there is any way around this.

It's pretty much a show stopper for us ...
 
Status
Not open for further replies.
Top