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

Fabrice La

Active Member
Licensed User
Longtime User
Hi

I created this program but the system never goes to SMTP1_MessageSent(Success As Boolean).
I confirm that android sent correctly the message but never goes to this event, no log(Success) ...
B4X:
Sub Activity_Resume
   SMTP1.Initialize(tmpST(0), tmpINT, tmpST(1), tmpST(3), "SMTP")
      If tmpST(4) = "true" Then
           SMTP1.UseSSL = True 
      Else
         SMTP1.UseSSL = False
      End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnEnvoyer_Click
   If File.Exists(File.DirDefaultExternal, "configEmail.ndf") Then
      SMTP1.To.Add(edbDestinataire.Text)
      SMTP1.Subject = edbSujet.Text
      SMTP1.Body = edbBody.Text
      If cbFrais.Checked Then
         SMTP1.AddAttachment(File.DirDefaultExternal, "file1")
      End If
      If cbIKM.Checked Then
         SMTP1.AddAttachment(File.DirDefaultExternal, "file2")
      End If
      SMTP1.Send
   Else
      Msgbox(Main.trans.GetText("Serveur de mail non configuré"), Main.trans.GetText("Attention"))
   End If
   
End Sub

Sub SMTP1_MessageSent(Success As Boolean)
    Log(Success)
    If Success Then
        ToastMessageShow("Message sent successfully", True)
    Else
        ToastMessageShow("Error sending message", True)
        Log(LastException.Message)
    End If
   activity.Finish
End Sub
 
Last edited by a moderator:

hanvas

New Member
Licensed User
Longtime User
Two Apps, one Service, one Problem

Hi,

dont know if this is the right place, it seems to be more a "service" question

At time i develop an Inhouse Application. The App send Informations about the Position, the Weight, Time, the arrival at a customers adress , the pickup or delivery of unspecified goods and somthing else.

The Apps sends their Information as a Text File to a FTP Server. Every two Minutes the Apps poll a directory on the Ftp Server an retrieves a List of Files to see if there are messages for the Driver or other important things available. During this process the App sends also same status informations.

I do this inside a service, to get not interrupted by the os. Even this works normaly fine.

Everything seems to run fine up to now.

I extendet the App starts Google Maps for Navigation via an intent. So when the pickup or delivery at a customers is finished, the driver presses a button. This sends the information about the departure to our server and starts Google Maps via an Intent to get navigated to the next customer. As of the moment where google maps is started and the topmost windows - the sending and retrieving of seems to be stopped. The Service i use to handle seems to sleep. It go to live again when i stop the google maps.

What can I do ?

Thanks

Ha-Joe
 

peacemaker

Expert
Licensed User
Longtime User
NNTP is also needed....
 

peacemaker

Expert
Licensed User
Longtime User
Thanks, Erel. If you are updating this lib, please, try to support NNTP protocol, at least some main base possibilities (receive X latest posts, send a post with an encoding).
 

vb1992

Well-Known Member
Licensed User
Longtime User
I was testing the FTP with:


Dim FTP As FTP

FTP.Initialize("FTP", "FTP.secureftp-test.com", 21, "test", "test")

FTP.List("/")



It never ends up getting to

Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
 

vb1992

Well-Known Member
Licensed User
Longtime User
Correct:

Bluestacks App Player for Windows PC

Doesn't work.

Moved it to my phone, and it worked.

Wow, that was a nice few hours wasted.
*edit: just taught me not to use blue stacks as my primary app player/tester

**edit 2: Btw, the thing that threw me off, was I was actually able to send this command: FTP.SendCommand("pwd","")
and it returned back a result in bluestacks... that's what threw me off, not getting the file listing...


Thanks!
 
Last edited:

Harris

Expert
Licensed User
Longtime User
SMTP Error

I download net 1.2 three times.... Checked declaration and code...
I get a compile error: "Input string was not in a correct format" on SMTP.Initialize.

I used the exact code from the example as well as my own. I have no idea what it is barking about.... It all looks so simple.

Any body had success using SMTP or had the same issue??

Thanks
 

Harris

Expert
Licensed User
Longtime User
Update.
Seems copying and pasting code from a pdf can introduce characters (like spaces and tabs) that you can't see in the IDE and throws it for a loop when compiling. Removed the crap it it all compiles... Thank heavens....
 

Harris

Expert
Licensed User
Longtime User
The last parameter of Initialize method is the EventName. If you set it to "SMTP" then the sub name should be: SMTP_MessageSent and not SMTP1_MessageSent

I recommend you to initialize SMTP in Activity_Create instead of Resume.

I have the same issue now... it never fires the event after SMTP1.send. Last param of Init is "SMTP1" and event is SMTP1_MessageSent.....

In the Code Mod declared SMTP1 as SMTP in Process_Globals.... (all code for sending mail is in the code mod)


:BangHead:
 

Harris

Expert
Licensed User
Longtime User
Thanks Erel, I knew that..:signOops: some time ago...

note to self: remember what code modules can handle waste less of every bodies time.
 

Harris

Expert
Licensed User
Longtime User
Ok, moved the event to activity mod and now send mail by SMTP.

Process:

I Init the SMTP object.
I take records from a table that have NOT been sent (SentDate = 0).
For each record, I extract a PDF from the blob to a file on disk - This is the attachment.
I send the mail (which does make it to the recipient - me) .
Once I receive confirmation from the event (Success = True), I update the associated record's SentDate with Now. I also delete the attachment pdf file from the disk to clean up.

The problem is that 10 records (mails) get sent in under 1 second (is this the non-blocking of which you speak?). When in actuality, these mails are que'd up and some underlying process is actually sending them. I have no idea if they did (actually) get sent - or not? There is no Progress (bar) on how each is being handled. In fact, if I drop the wifi connection, I get success results for all when some or all have not been sent.

However, if I try to start a send session without a wifi connection, I get "no sever found" - which is quite logical and expected.

How can I get verification that each mail was actually sent? I cannot correctly update the table without knowing the mail DID make it out the door.

Thanks so much...
 

Harris

Expert
Licensed User
Longtime User
Yes, waiting for messagesent is the approach I am trying - however Success is all I have to go on which happens immediately after issuing send (if a server is present). There is no way my mail and its' (rather larger) attachment get sent immediately. I was hoping for the ack after the mail (in its' entirety) had been accepted by the server - or would this be considered blocking - holding everything up waiting for a response from the server - or determining when the last byte of the message went out?

Thanks Erel
 

Harris

Expert
Licensed User
Longtime User
Geez Erel, now I can't get it to send at all... Didn't change anything and now it reports Authenticaion required - SSL is True... (SMTP1.UseSSL=True). I will let you know when I can get past this.... Thanks

I sent mail to support for a copy of 1.8 btw
 
Status
Not open for further replies.
Top