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

biometrics

Active Member
Licensed User
Longtime User
I did a test with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim ps As PhoneWakeState
   ps.KeepAlive(True)
   FTP.Initialize("ftp", "xxx", 21, "aaa", "bbb")
   FTP.DownloadFile("/xxx", False, File.DirRootExternal, "1.dat")
End Sub

Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
   Log(NumberFormat2(TotalDownloaded, 0, 0, 0, True))
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
   Log(ServerPath & " " & Success)
End Sub
It successfully downloaded a 163MB file. DownloadCompleted fired at the end. I ran it in rapid debug mode.

Hi Erel,

I'm having this problem of the event not firing on other devices as well for example the VIA VT-6078 with Android 4.0.3. So it's not only the Samsung Galaxy Tab 3 10.1.

Even though I have a workaround it's not ideal and wastes time & resources.

Is there anyway for you to fix this?

(btw I'm using FTPS)
 

biometrics

Active Member
Licensed User
Longtime User
I'm sorry but I was unable to reproduce it. I tested it on several devices.
Hi Erel,

Thanks for the effort.

I think I have a clue for you how to replicate it ...

I think it is related to how long the download takes, not necessarily the file size. Small files which download under a minute works fine (the event fires) but large files which take about 20 minutes fails (event doesn't fire).

We only have a shared 4 Mbps connection at our office (old part of the city with old infrastructure) so it takes a while to download.

To replicate the problem, you'll need to either throttle your connection speed, or use a slow connection, or easiest is to just use a large file of a couple of GB. Basically make the download take 20 minutes.
 

Jausa

Member
Licensed User
Longtime User
¡Hello!
How can i read multiple files from a folder and add them as attachmets.

I know that i just have to use the SMTP.AddAttachment(Route, Name) Statement
But i don't know how to read all the files from that route.

¿Some For Each statement?
¿Any example?
 

M.LAZ

Active Member
Licensed User
Longtime User
:sign0148:,

don' t know where i got the protocol from??? I think it was when i looked up Gmail requirements.

Anyway i changed to "SMTP1" and it works beautifully.

Thank you

FWIW to any others, I changed all the code and put it into a Service Module and it runs great in the background. The program keeps running whilst all emails are being silently sent.


he
wow great, how did you put the code into a service module?
 

Shadow&Max

Active Member
Licensed User
Longtime User
This is working perfectly for me. However, I'm only using the net library to send one anonymous email from the program once, and the "Full Network Access" permission will be very scary for my users when the app gets out there this weekend or on Monday.

Is there anything I can use, other than this net library, that will allow me to send the anonymous email without the horrendous, scary "Full Network Access" permission?

If not, I'll have to try to figure out another way to do this because I think I'll lose buyers with the permission...
 

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks Erel... yes, I tried removing it, and the Full Network Access is still there. Which one of these libs is causing this? Is it the Phone????

Camera
Content Resolver
Core
DateUtils
Dialogs
Gestures
JavaObject
Net
Notification Builder
Phone
RandomAccessFile
Reflection
SQL
TTS

OK, I did click the List Permissions button, and I see Internet access for Webviewer and SMTP... Is that what's generating the "Full Network Access" permission on my Moto-X and on the emulator? Seems like a pretty frightening permission, although I haven't installed a lot of other apps on my own phone...

If this is a "standard" permission of most apps, then I won't worry about it.
 
Last edited:

Shadow&Max

Active Member
Licensed User
Longtime User
Thanks again Erel... Yes, I did that and figured out all of the permissions. Thank you.

It also occurred to me that most apps will require network access, so I'm not going to worry about it.

Thank you again.
 

Mahares

Expert
Licensed User
Longtime User
I use pop3 mailparser to download attachments to a given folder in the device. I use an input box to enter the device’s owner password before the initialization process.

How can I verify that the password entered matches the owner’s stored password, so I can use it in the initialization line below without having to look it up inside a text file or map:

pop.Initialize("pop.gmail.com", 995, AccountName, MyPassword, "pop")
 

Mahares

Expert
Licensed User
Longtime User
You can call pop.ListMessages. If it succeeds then the user name and password are correct.
How can you do a pop.Listmessages before the pop is initialized? I need to find out what the password is before initialization so I can put in this code: pop.Initialize("pop.gmail.com", 995, AccountName, MyPassword, "pop")
Extracting it from a file is not safe.

SOLVED by adding this code:
B4X:
Sub Activity_Resume
    If pop.IsInitialized=False  Then
        pop.Initialize("pop.gmail.com", 995, accountName, MyPassword, "pop")
        pop.useSSL = True  'very important must be below the initialize line
    End If
End Sub

However, I can ONLY use a Gmail account to send and receive in order for the attachments to save. If POP3 mailmarper works with other email clients, I have not been able to make it work.
Thank you
 
Last edited:
Status
Not open for further replies.
Top