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:

rleiman

Well-Known Member
Licensed User
Longtime User
I just put some coding in the app to catch errors.

I get an error stating:

B4X:
Empty writer returned
Authentication Required

How do I code for Authentication?

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Got it working. RFman had the same problem so I did what RFman did:

From RFman
Ater getting this error, got a gmail indicating that if I want my app to have access to sent gmail to change my security settings at the link provided. I did so, and the gmail now works with my app.
 

GDO

Member
Licensed User
Longtime User
Hello,
I 've got a trouble with POP3.
I can't receive message bigger than 1.4 Mb and the trouble is, there is no error message or other informations to help me.
I use the lastest net library with the example from Erel and a Gmail account (but same trouble with other provider).
Any idea ?
Or could you please make a test with a message and 2 Mb jpeg attachment ?
Thanks.
 

GDO

Member
Licensed User
Longtime User
Was the DownloadCompleted event raised?

Have you checked the unfiltered logs?

I've seen nothing special in logs (nothing more with or without the trouble).
And No, DownloadCompleted event is never raised when trouble (with 1.5 Mb attachment or biger), nothing happens.

is it working for you with 1.5 Mb ?
 

DonManfred

Expert
Licensed User
Longtime User
What's wrong with me ?
You are not showing the code you are using. Without seeing it we just could guess.

glaskugel.jpg
 

GDO

Member
Licensed User
Longtime User
I am going crazy ...
The good news, it's working with 1.5 Mb attachment. Why ? I don't know ...

I made a bunch of tests and sometimes it's good, sometimes it is not !?
I suppose when something is going wrong the others tests are meanless.

So I tested again and again with reboot everytime something is wrong.

Now, it's working with 4.5 Mb attachment (in fact no more than 5 Mb for the whole mail) and I think it is ok according to the provider.

The trouble is, but I can live with it, when I try to download mails in a loop, sometimes it's ok but sometimes the big one is not ok.
So there is a limit for one mail and I think that there is a limit for a group of mails in one session.

Any idea ?

Anyway, Thank you for your help.

My code, from your code :

#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: POP3 Example
#VersionCode: 2
#VersionName: 1.01
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
Dim pop As POP3
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
pop.Initialize("pop.gmail.com", 995, "[email protected]", "xxx", "pop")
pop.UseSSL = True
End If
pop.ListMessages
End Sub

Sub POP_ListCompleted (Success As Boolean, Messages As Map)
If Success = False Then
Log(LastException.Message)
Else
'download all messages
'change last parameter to True if you want to delete the messages from the server
For i = 0 To Messages.Size - 1
pop.DownloadMessage(Messages.GetKeyAt(i), False)
Next
End If
pop.Close
Log("Terminé")
End Sub

Sub POP_DownloadCompleted (Success As Boolean, MessageId As Int, MessageText As String)
If Success = False Then
Log(LastException.Message)
Else
'Parse the mail
' Dim m As Message
Try
' m = MailParser.ParseMail(MessageText, File.DirRootExternal)
' Log(m.DateField & CRLF & m.FromField & CRLF & m.Subject & CRLF & m.Body & CRLF & m.Attachments)
' ToastMessageShow(m.DateField & CRLF & m.FromField & CRLF & m.Subject & CRLF & m.Body & CRLF & m.Attachments, True)

File.WriteString(File.DirRootExternal, "toto" & MessageId & ".txt", MessageText)
' Log(File.Size(File.DirRootExternal, "toto" & MessageId & ".txt"))
ToastMessageShow("FICHIER SAUVEGARDé --------------------", True)
Catch
Log(LastException.Message)
End Try
End If
End Sub

Thanks again !

:)
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello,

I have a problem sending Mail using SMTP:

the body of the sended mail is always in the form:

Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

is it possible to send mail where the body is encoded as:

Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

Thanks,
 

RobM

Member
Licensed User
Longtime User
Some users of my app (3 out of 10) are reporting this error when sending an email with SMTP.

org.apache.commoms.net.smtp.SMTPConnectionClosedException: Connection closed without indication.

They have tried sending with and without wifi. The email in question is a html table 8kb in size no attachments.
The same email can be sent ok using the phones default email client.
I cannot reproduce the error.

Any thoughts?
 

RobM

Member
Licensed User
Longtime User
All the settings are done in the app. The user supplies their user name and password.

The same credentials are used to download emails, which is working ok.

The users all work for the same company and have an office 365 account. All have the same phone.

Would increasing the timeout help ?

Edit:
A user of the app was on a clients site connected to his WiFi. He got the same error when trying to send. Disconnecting from the Wifi and sending via 3g worked for this user.
Don't know if this is of any help.
 
Last edited:

RobM

Member
Licensed User
Longtime User
Re Reply 334 and 336
Still having issues with some users not being able to send via SMTP to the office365 server. Can download ok.
Users can send if they use gmail SMTP instead. (Much quicker to send)

This is the reply from IT services.

Hi Rob,
It’s a MS hosted system that we already have working for other devices in the business. The chances of the issue being server side are slim to nothing. The problem will be with the app.
It’s possible the correct SSL/TLS isn’t supported by the app and is thus being dropped by MS. Beyond that I can’t really help sorry.


I do have the gmail send option as a workaround, but having users enter 2 sets of credentials is a bit messy.

One last plea for any suggestions.
 

RobM

Member
Licensed User
Longtime User
Unfortunately due to company policy it would not be possible to have access.

Thanks
 
Status
Not open for further replies.
Top