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

Attachments

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

jrat

Member
Licensed User
Longtime User
I need change FTP server in settings in my program. In tutorial i see the adress of server set only in Initialize function? and i not understand how change them after.
 

Mike Maurice

Member
Licensed User
Longtime User
Net.zip

It would help a lot if the install instructions were included in the Zip file as there are is more than one place where the zip file link can be found and it does not have the install instructions; which requires one to then search around to find them.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm getting:

org.apache.commons.net.MalformedServerReplyException: Received invalid POP3 protocol response from server.220 mx.google.com ESMTP gs6sm1085265igc.11



Any thoughts?
 

isr

Member
Licensed User
Longtime User
Unable to get FTP in the NET library to work

I am trying to FTP a SQLite database created by my app to an FTP server. I have tried three separate FTP servers without success. Each time, it seems as if my phone is transmitting as indicated by the upload arrow on my notification bar. However, I never receive the file at the FTP servers, and the FTP log I'm able to access at one of the servers doesn't show any sign of the transfer attempt.

Here is the code I'm using:
B4X:
Dim ftp as FTP
ftp.Initialize("ftp", "ftp.myserver.com", 21, "user", "password")
ftp.UseSSL = True
ftp.PassiveMode = True
ftp.UploadFile("/sdcard/Android/data/com.myapp.android/files", "sqlite.db")
ftp.Close

I've tried this while commenting out either or both of UseSSL and PassiveMode, and it still is not successful.

If I turn off the filter in the log, in debug mode I get the following error message when the file upload should be occurring:
"Using factor org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl@401cefb0
Unknown error 1 during connect"

I imagine there is something simple that I am doing wrong, but I couldn't find in the forum or the documentation an example of an actual project with FTP in the NET library to use as a model.

Thank you for any advice you might have!
 

isr

Member
Licensed User
Longtime User
Unable to get FTP in the NET library to work

I've searched online (especially at StackOverflow) and found out that the "unknown error 1 during connect" refers to a problem with the SSL handshake.

I've played around with the settings more (I made some errors in my prior code) and have finally been able to FTP successfully _without_ SSL. All the FTP servers I am using explicitly recommend using SSL to connect.

Here's the code for my successful insecure FTP uploads:
B4X:
Dim ftp as FTP
ftp.Initialize("ftp", "ftp.myserver.com", 21, "user", "password")
ftp.UseSSL = False
ftp.PassiveMode = True
ftp.UploadFile(File.DirDefaultExternal, "sqlite.db", True, "sqlite.db")
ftp.Close

So my remaining problem is that UseSSL just isn't working on three different, independently operated FTP servers.

If I can't get SSL to work, is my only option to encrypt the database and FTP the file without SSL?

Edit: I've just tried to open the database I successfully send by FTP without SSL. However, when I use SQLite Database Browser on PC, the database shows no tables (although it has several). The file size registers correctly, though. When I try to compact the database in SQLite Database Browser (just to try some operation on it to get some sort of reaction), I receive the error message "database disk image is malformed." Keep in mind this is a database that my B4A app created.

I can open the database on the phone with an SQLite viewer (aSQLiteManager). Also, I can open the database through SQLite Database Browser on my PC when I connect my phone to my PC via USB.

Does FTPing the database file somehow corrupt it?
 
Last edited:

isr

Member
Licensed User
Longtime User
Thank you, Erel, for your response.

I did make a mistake with setting ASCII mode to "True". When I fix that (by setting it to "False"), the database is readable after sending by FTP. So that problem is resolved.

However, I can only FTP the file successfully without SSL (by setting UseSSL to False, or by not even including the UseSSL statement). I still get the same "Unknown error 1 during connect" error when I use SSL. As I mentioned, the FTP servers I am using explicitly recommend using SSL for the transfer. For example, here is the recommendation for one FTP server service:
"When configuring your FTP client connection, choose FTP over explicit TLS/SSL (FTPES)".

Edit: The problem seems to be on the Android/B4A side. I've successfully sent the file from my phone to this FTP server by FTP over SSL with an Android FTP client (AndFTP), using the same settings as in my app.

What else can I do to troubleshoot this?

Thanks again!
 
Last edited:

isr

Member
Licensed User
Longtime User
Erel,

Here is the section of the unfiltered log where the FTP attempt occurs.

Log excerpt:

"UpdateRecord: UPDATE ...
GC freed 9824 objects / 545168 bytes in 112ms
Using factory org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl@401cefa8
cdmaLevel:0
iconLevel:0
Unknown error 1 during connect
cdmaLevel:-1
iconLevel:-1
cdmaLevel:2
iconLevel:2
cdmaLevel:2
iconLevel:2
notifyDataConnection() state=2isDataConnectivityPossible()true, reason=null
broadcastDataConnectionStateChanged() state=CONNECTEDtypes=default,supl, interfaceName=rmnet0
onDataConnectionStateChanged 7"

The log above the first line documents numerous database operations (updates, inserts, etc.) and other events (more than 1500 lines). I've put an ellipsis to replace table names.

The only difference between the log output here, when the FTP fails (because UseSSL is set to True), and when the FTP succeeds (because UseSSL is set to False) is the "Unknown error 1 during connect" line.

Thank you!
 

louisproy123

Member
Licensed User
Longtime User
I bought this CHIP from Korea ( from company WIZNET WizFi220 )

and it allows WIFI to Serial communication .....
Do you have examples of WIFI SOCKET communication using RAW TCP ?

Thanks,:sign0085:
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Are you using the correct port? Should be:
B4X:
pop.Initialize("pop.gmail.com", 995, "aaa@gmail.com", "bbb", "eventname")
pop.UseSSL = True

I thought I was, turns out I was using the SMTP settings for POP3
That fixed it, thanks
 

isr

Member
Licensed User
Longtime User
I've decided to abandon FTP as a way for sending files from the phone to a server. Apart from my troubles with SSL and FTP, one of my testers was unable to transmit successfully even though he had a good connection (and this was without SSL). I then searched the forums more thoroughly and found that many others have had unpredictable troubles with FTP as well.

So, I've decided to try PostFile in HttpUtils (see this helpful thread:
http://www.b4x.com/forum/basic4android-updates-questions/22132-httputils2-simple-image-upload.html#post128328).

Thanks again, Erel, for considering my problem.
 

fabpalma

Member
Licensed User
Longtime User
SMTP problem

Hi Erel,

I'm trying to use SMTP library to send automatic email from my app.
It work well if I use gmail settings.
I tried to use smtp.live.com with my outlook.com account and I'm unable to send any email. I tried with my Yahoo account and it work perfectly !!

So, I setup a SMTP service on my web server (Windows 2003 server) and I tried to send mail using a simple, little client written in C# that simply initialize the object with username and password (credential), setup the host address and the port (25) and deselect "Use SSL).
The C# client run perfectly and I can send my email.

But I'm unable to send email using my Android device with the same settings.
The answer that I see using Log(LastException.Message) in SMTP_MessageSent event is:

Success = False
java.lang.RuntimeException: Empty writer returned: 530 5.7.3 Client was not authenticated

I tried with several username, with and without domain name but without success. I tried with and without SMTP.Sender field with username, username@domain.tld ... No success !!!

With my C# client, I use only username without "@domain.tld" ...

This is the code:
'setup smtp server
SMTP.Initialize("xxx.xxx.xxx.xxx", 25, "myuser", "mypass", "SMTP")
SMTP.UseSSL = False

' this is gmail setup that work correctly
'SMTP.Initialize("smtp.gmail.com", 465, "myname@gmail.com", "mypass", "SMTP")
'SMTP.UseSSL = True

'sender
SMTP.Sender = Chr(34) & "Automail" & Chr(34) & " <automail@domain.tld>"

SMTP.To.Add(Global.MailAddress)
SMTP.Subject = "Test message"
SMTP.Body = "This is a test mail message sent from Android application." & Chr(13) & Chr(10)

SMTP.Send

Any idea ?

Thanks a lot.
Fabrizio

PS: my server use plain text authentication methode without SSL.
PPSS: if you need, I can setup an account with your name for your testing...
 

AscySoft

Active Member
Licensed User
Longtime User
I know this thread is so long, but something that I've miss before :D got my attention.
Do you have an code example in b4a that use
B4X:
myftp.UseSSL=true
Is this the same with ftps (FTP over SSL)? I guess it is!
Then how to set custom command port / data port?
Basically I ned an example of a connection to a ftp(s) server, list, download,upload etc
Without SSL my program work just fine. But for security reasons I would like to incorporate this(I know I must change my server settings, I know how to do this etc)
 
Status
Not open for further replies.
Top