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,501
Last edited:

Mikonios

Active Member
Licensed User
Longtime User
Hi,

I need capture FTP1.SendCommand("CD", DirFtp) response.

This code no work ::
B4X:
Sub FTP_CommandCompleted (ServerPath As String, Success As Boolean)
    If Not(Success) Then
        Log("SendCommandComplete failed : " & LastException)
        Csr.LogMemory1("   X02_MySndCmd   :: (Err)", "3")
        X55_SndMyFtpSale("    FTP_SendCommandComplete NoControlado")
    Else
        Log("SendCommandComplete Ok: ")
        X55_SndMyFtpSale("    FTP_SendCommandComplete Ok")
    End If
End Sub

And can't find xample documentation for FTP_CommandCompleted ::
((( https://www.b4x.com/android/help/net.html#ftp_sendcommand )))
SendCommand (Command As String, Parameters As String) Sends an FTP command.
The CommandCompleted event will be raised with the server reply. Should only be used with commands that return the reply in the command channel (not the data channel).
It is possible that Success will be false and LastException will not be initialized.

Anywhere can say me what´s correct sintax, or mode to get response, Pleased ??
 
Last edited:

Mikonios

Active Member
Licensed User
Longtime User
Ok, this is SOLUTION my problema :::

B4X:
Sub FTP_CommandCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    If Not(Success) Then
        Log("SendCommandComplete failed : " & LastException)
        Csr.LogMemory1("   X02_MySmbPut.Upload    :: (Err)", "3")
        X55_SndMyFtpSale("    FTP_SendCommandComplete NoControlado")
    Else
        Log("SendCommandComplete Ok: ")
        X55_SndMyFtpSale("    FTP_SendCommandComplete Ok")
    End If   
End Sub
 

DonManfred

Expert
Licensed User
Longtime User

JohnC

Expert
Licensed User
Longtime User
I just verified (using android 5.0+ and this latest NET lib) that when a file is being transferred (uploading from device), the UI of my app hangs until the file is completed. I thought that the transfer is executed in a background thread, so why would it hang my apps' UI?
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
Everything freezes.....the horizontal scrolling of a table, pressing buttons, everything.
 

DonManfred

Expert
Licensed User
Longtime User
Do you block the mainthread?
How does your code looks like?


This works for me without blocking anything

B4X:
    If FirstTime Then
        'FTP.Initialize("FTP", "hdro-die-schwarze-hand.de", 21, "w00d7468", "3BzsLDPeoWDJWCVn")
        FTP.Initialize("FTP", "host", 21, "user", "password")
    End If
   
    Dim flist As List = File.ListFiles(File.Combine(File.DirRootExternal,"Download"))
    Log(flist)
    Log(File.Combine(File.DirRootExternal,"Download"))
    FTP.UploadFile(File.Combine(File.DirRootExternal,"Download"),"test.iso",False, "/test.iso")
    'FTP.UploadFile(File.DirAssets,File.Combine(File.DirRootExternal,"Download/test.iso"),False, "/test.iso")
End Sub

Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
  s = "Uploaded " & (TotalUploaded / 1024 / 1024) & "MB"
  If Total > 0 Then s = s & " out of " & (Total / 1024 / 1024) & "MB"
  If s.EqualsIgnoreCase(old) Then
    Else
        old = s
        Label1.Text = s
        Log(s)
    End If
End Sub
Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
  Log(ServerPath & ", Success=" & Success)
  Label1.Text = "Upload (18mb) completed!"
    If Success = False Then Log(LastException.Message)
End Sub
 

JohnC

Expert
Licensed User
Longtime User
This is my upload progress event sub - it shouldn't hang anything:

B4X:
Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
    Dim s As String
    s = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s & "(" & ServerPath & ")")
End Sub
 

JohnC

Expert
Licensed User
Longtime User
Erel,

My client and I can reproduce this condition at will and on multiple devices (some android 5.0 the others 6.0).

I am using your latest "NET" lib (v1.62).

If this makes a difference, the FTP.Initialize line and it's event subs are in a service module because I need to have the FTP happen in the background while the user is using another activity.

How can I prevent an FTP transfer from locking the UI of another activity?
 

rtek1000

Active Member
Licensed User
Longtime User
Hello guys,

SMTP from Gmail is impossible now for me.

The response error received on log redirects to page treatment of incorrect/insecure login, and offers an alternative from page:
https://support.google.com/accounts/answer/6010255?hl=en

"some apps or devices that do not use modern security standards"

To turn more insecure this acount but yet can not send any e-mail.

I try both ways, TLS and SSL.

Can you post a functional sample, please?
 

rtek1000

Active Member
Licensed User
Longtime User
Hello again,

SMTP works fine now,
I made less secure options again:

https://www.google.com/settings/security/lesssecureapps

and I changed password too.

B4X:
Sub Send_Message(Msg1 As String)
   Dim SMTP1 As SMTP
   SMTP1.Initialize("smtp.gmail.com", 465, "[email protected]", "password", "smtpEventResponse")
   SMTP1.To.Add("[email protected]") 'change to real address
   SMTP1.Subject = "Test1"
   SMTP1.Body =  "123123123"
   SMTP1.UseSSL = True
   SMTP1.Send
End Sub
Sub smtpEventResponse_MessageSent(Success As Boolean)
   Log(Success)
   Log(LastException)
End Sub

Thank you.
 

rtek1000

Active Member
Licensed User
Longtime User
Hello,

It would be possible to reverse engineer the gmail app to see how it is modern authentication, or it is legally prevented?
 

Don Oso

Active Member
Licensed User
Longtime User
The SMTP always sends the AUTH command. Do you get any error in MessageSent event?

Yes I get errors when i try to send a message without AUTH or with empty values in user and password parameters . but i wonder maybe there another method to send messages without auth

I think that the best soluton here , its put authentication to the local SMTP server in my case. o_O

thanks
 
Status
Not open for further replies.
Top