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

Philip Prins

Active Member
Licensed User
Longtime User
You might need to allow access to "less secure" apps in your gmail account.

I used the SMTP function 1.54 with B4A but since the 1st of september 2015 i get the following error when sending email with gmail.

Delivery to the following recipient failed permanently:

emailadress@outlook.com

Technical details of permanent failure:
Internal parse error: Illegal envelope To: address (not valid RFC5321 syntax): emailadress@outlook.com
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested it with this code and it sent successfully:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim s As SMTP
   s.Initialize("smtp.gmail.com", 465, "xxx@gmail.com", "xxxx", "s")
   s.To.Add("test@test.com") 'change to real address
   s.Body = "123123123"
   s.UseSSL = True
   s.Send
End Sub

Sub s_MessageSent(Success As Boolean)
   Log(Success)
   Log(LastException)
End Sub
 

ivanomonti

Expert
Licensed User
Longtime User
I tried them all with gmail, but the result is always false, not send


B4X:
Sub btalert_click
   
    Dim bt As Button = Sender
    Dim ls As List = bt.Tag
   
    If ls.Get(0) = "" Or ls.Get(1) = "" Or ls.Get(2) = "" Then
        Msgbox("Compilare tutti i campi, email, telefono e nome, grazie per la vostra attenzione.", "Attenzione")
        Return
    End If
   
    Dim SMTP As SMTP
    SMTP.Initialize("smtp.gmail.com", 587, "xxxxxxxxxxxxxxx@gmail.com", "xxxxxxxxxxxxxxxxxx", "SMTP")
    SMTP.StartTLSMode = True
   
    Log(ls.Get(0) & " " & ls.Get(1) & " " & ls.Get(2))

    Try
        'SMTP.UseSSL = True
        SMTP.To.Add(ls.Get(0))
        SMTP.BCC.Add("ivanomonti@gmail.com")
            SMTP.Subject = "Informazioni generali del servizio"
        SMTP.Body = "Desidero avere più informazioni a riguardo il progetto Human Equality" & Chr(10) & "Mio contatto telefonico: " & ls.Get(1) & Chr(10) & "Mio nome: " & ls.Get(2) & Chr(10) & "Cordiali saluti a sentirci presto"
        'SMTP.AddAttachment(File.DirRootExternal & "/Encuestas", exportfile)
        SMTP.Send
    Catch
        Log("Text file not sent")
    End Try
   
End Sub

Sub SMTP_MessageSent(Success As Boolean)
  If Success=True Then Msgbox("Il vostro messaggio è stato inviato correttamente, grazie per la fiducia dataci.", "Human Equality")
  If Success=False Then Msgbox("Il vostro messaggio non è stato inviato correttamente, riprovi più tardi.", "Human Equality")
End Sub
 

ivanomonti

Expert
Licensed User
Longtime User
I tried them all with gmail, but the result is always false, not send


B4X:
Sub btalert_click
  
    Dim bt As Button = Sender
    Dim ls As List = bt.Tag
  
    If ls.Get(0) = "" Or ls.Get(1) = "" Or ls.Get(2) = "" Then
        Msgbox("Compilare tutti i campi, email, telefono e nome, grazie per la vostra attenzione.", "Attenzione")
        Return
    End If
  
    Dim SMTP As SMTP
    SMTP.Initialize("smtp.gmail.com", 587, "xxxxxxxxxxxxxxx@gmail.com", "xxxxxxxxxxxxxxxxxx", "SMTP")
    SMTP.StartTLSMode = True
  
    Log(ls.Get(0) & " " & ls.Get(1) & " " & ls.Get(2))

    Try
        'SMTP.UseSSL = True
        SMTP.To.Add(ls.Get(0))
        SMTP.BCC.Add("ivanomonti@gmail.com")
            SMTP.Subject = "Informazioni generali del servizio"
        SMTP.Body = "Desidero avere più informazioni a riguardo il progetto Human Equality" & Chr(10) & "Mio contatto telefonico: " & ls.Get(1) & Chr(10) & "Mio nome: " & ls.Get(2) & Chr(10) & "Cordiali saluti a sentirci presto"
        'SMTP.AddAttachment(File.DirRootExternal & "/Encuestas", exportfile)
        SMTP.Send
    Catch
        Log("Text file not sent")
    End Try
  
End Sub

Sub SMTP_MessageSent(Success As Boolean)
  If Success=True Then Msgbox("Il vostro messaggio è stato inviato correttamente, grazie per la fiducia dataci.", "Human Equality")
  If Success=False Then Msgbox("Il vostro messaggio non è stato inviato correttamente, riprovi più tardi.", "Human Equality")
End Sub

error:
530 5.5.1 https://support.google.com/mail/answer/14257 qq4sm10900719wjc.14 - gsmtp
 

ivanomonti

Expert
Licensed User
Longtime User
change service SMTP

setting service operatgor

B4X:
'    Secure SSL/TLS Settings
'    (Recommended)
'    Username:    info@humanequality.eu
'    Password:    Use the email account’s password.
'    Incoming Server:    gator4098.hostgator.com
'    IMAP Port: 993
'    POP3 Port: 995
'    Outgoing Server:    gator4098.hostgator.com
'    SMTP Port: 465
'    Authentication Is required For IMAP, POP3, And SMTP.

Code B4x

B4X:
Sub btalert_click
   
    ProgressDialogShow("Send Message...")
   
    Dim bt As Button = Sender
    Dim ls As List = bt.Tag
   
    If ls.Get(0) = "" Or ls.Get(1) = "" Or ls.Get(2) = "" Then
        Msgbox("Compilare tutti i campi, email, telefono e nome, grazie per la vostra attenzione.", "Attenzione")
        Return
    End If
   
'    Secure SSL/TLS Settings
'    (Recommended)
'    Username:    info@humanequality.eu
'    Password:    Use the email account’s password.
'    Incoming Server:    gator4098.hostgator.com
'    IMAP Port: 993
'    POP3 Port: 995
'    Outgoing Server:    gator4098.hostgator.com
'    SMTP Port: 465
'    Authentication Is required For IMAP, POP3, And SMTP.

    Dim SMTP As SMTP
    SMTP.Initialize("gator4098.hostgator.com", 465, "info@humanequality.eu", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "SMTP")
    SMTP.StartTLSMode = True
   
    Log(ls.Get(0) & " " & ls.Get(1) & " " & ls.Get(2))

    Try
        SMTP.UseSSL = True
        SMTP.To.Add(ls.Get(0))
        SMTP.BCC.Add("info@humanequality.eu")
            SMTP.Subject = "Informazioni generali del servizio"
        SMTP.Body = "Desidero avere più informazioni a riguardo il progetto Human Equality" & Chr(10) & "Mio contatto telefonico: " & ls.Get(1) & Chr(10) & "Mio nome: " & ls.Get(2) & Chr(10) & "Cordiali saluti a sentirci presto"
        SMTP.Send
    Catch
        Log("Text file not sent")
    End Try
   
End Sub

MessageSend

B4X:
Sub SMTP_MessageSent(Success As Boolean)
    ProgressDialogHide
      If Success=True Then Msgbox("Il vostro messaggio è stato inviato correttamente, grazie per la fiducia dataci.", "Human Equality")
      If Success=False Then Msgbox(LastException, "Human Equality")
    Log(LastException)
End Sub

Error LastException

B4X:
(SocketTimeoutException) java.net.SocketTimeoutException
 

ivanomonti

Expert
Licensed User
Longtime User
You need to allow unsecure connections to gmail in your gmail account.

Here I changed the mail server is the problem remains


' Secure SSL/TLS Settings
' (Recommended)
' Username: info@humanequality.eu
' Password: Use the email account’s password.
' Incoming Server: gator4098.hostgator.com
' IMAP Port: 993
' POP3 Port: 995
' Outgoing Server: gator4098.hostgator.com
' SMTP Port: 465
' Authentication Is required For IMAP, POP3, And SMTP.

B4X:
DimSMTPAsSMTPSMTP.Initialize("gator4098.hostgator.com", 465, "info@humanequality.eu", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "SMTP")
SMTP.StartTLSMode = True
 

Greg Shannon

Member
Licensed User
Longtime User
Hi,

I've been using this library successfully to emails from various email accounts. Recently (last few months) I've been having trouble with gmail, the problem is that only the first receipent will receive the email. The email will not get sent to any additional receipent, doesn't matter if the additional receipents are in the To, CC, or BCC fields.

When sending using hotmail, yahoo and other's I've tried, all receipents will receive the emails, this is just a problem with gmail.

Allow unsecure connections is turned on on the gmail account.

I've tried port 465 with UseSSL = true and port 587 with StartTLSMode = true, in both cases the email is sent successfully to only the first receipent.

In my sent folder in gmail, I can see the message and it shows a list of all the receipents that it should of been sent to.

No failure message of any kind is received.

Any ideas? Is anybody else getting the same results when using gmail?
 

Greg Shannon

Member
Licensed User
Longtime User
Thanks you for your response, knowing it work let me know I was doing something wrong. It turned out that my email addresses started with a space, as I was spliting them out of one string to add them to the receipents list. Triming the email address before adding them fixed the problem. I guess gmail no longer supports the white space before the email address.
 

rleiman

Well-Known Member
Licensed User
Longtime User
I started to use the Net library to send an email from my app then Google sent me an email stating I should use a secure app like the gmail app. Google stopped my app from signing into my gmail account.

How can I bypass this restriction?

Here is the coding I'm using (password and email is not the real ones)

In Sub Globals:

B4X:
    Private Email As SMTP

In Sub Activity_Create:

B4X:
    Email.Initialize("smtp.gmail.com", 587, "myemail@gmail.com", "mypassword", "SMTP")
    Email.StartTLSMode = True

B4X:
    Email.To.Add("someonesemail@gmail.com")
      Email.Subject = "This is the subject"
      Email.Body = "This is the message body."
      Email.AddAttachment(File.DirRootExternal, "apdfdocument.pdf")
      Email.Send

How can I code this so Google will allow me to send emails through the app?
 

DonManfred

Expert
Licensed User
Longtime User

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Don,

I've tried port 465 with UseSSL = true and port 587 with StartTLSMode = true but Google still sends the email that I'm blocked from using gmail in my app to access my gmail account to send the email.

Do you have a small working sample app (leaving out your email and password) I can test by replacing those details with mine?

Looks like Google has implemented some new security.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Don,

I looked and tried what other people have done without success. I hope someone who recently used gmail and this lib will post some sample code.
 
Status
Not open for further replies.
Top