Android Programming Press on the image to return to the main documentation page.

NetExtras

Written by B4A Net library modified by Martin Pearman

NetExtras is a modified version of the official B4A Net library.
FTPResume is a modified version of the FTP Object.
It's DownloadFileResume replaces DownloadFile and it's UploadFileResume replaces UploadFile.
Both of these two new methods support resume of incomplete file download and upload.
SMTPExtras is a modified version of the SMTP object.
It has been modified to allow additional (custom) header fields to be added to the outgoing email,
and control over the connection timeout periods.
NetExtras is based on version 1.52 of the B4A Net library.

List of types:

CustomTrustManager
FTPEntry
FTPResume
SMTPExtras

CustomTrustManager

CustomTrustManager allows you to create a SSL trust manager from a cert file or to create a trust manager that accepts all certificates.

Events:

None

Members:


  Initialize (Dir As String, FileName As String)

  InitializeAcceptAll

  IsInitialized As Boolean

Members description:

Initialize (Dir As String, FileName As String)
Initializes the trust manager based on the given cert file.
InitializeAcceptAll
Initializes an "accept all" trust manager. This option should only be used in safe networks as it offers no real protection.
IsInitialized As Boolean

FTPEntry

FTPEntry represents a file or a folder. Call FTP.List to get the files and folders.

Events:

None

Members:


  IsInitialized As Boolean

  Name As String [read only]

  Size As Long [read only]

  Timestamp As Long [read only]

Members description:

IsInitialized As Boolean
Name As String [read only]
Size As Long [read only]
Timestamp As Long [read only]

FTPResume


Permissions:

android.permission.INTERNET

Events:

CommandCompleted (Command As String, Success As Boolean, ReplyCode As Int, ReplyString As String)
DeleteCompleted (ServerPath As String, Success As Boolean)
DownloadCompleted (ServerPath As String, Success As Boolean)
DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
UploadCompleted (ServerPath As String, Success As Boolean)
UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)

Members:


  Close

  CloseNow

  DeleteFile (ServerPath As String)

  DownloadFileResume (ServerFilePath As String, AsciiFile As Boolean, DeviceFolder As String, DeviceFile As String, RestartOffset As Long)

  Initialize (EventName As String, Host As String, Port As Int, User As String, Password As String)

  IsInitialized As Boolean

  List (ServerPath As String)

  PassiveMode As Boolean

  SendCommand (Command As String, Parameters As String)

  SetCustomSSLTrustManager (TrustManager As Object)

  UploadFileResume (DeviceFolder As String, DeviceFile As String, AsciiFile As Boolean, ServerFilePath As String, RestartOffset As Long)

  UseSSL As Boolean

  UseSSLExplicit As Boolean

Members description:

Close
Closes the connection after all submitted tasks finish. Note that this method does not block.
CloseNow
Closes the connection immediately without waiting for current tasks to finish.
The data connection will only be closed when UploadProgress or DownloadProgress events fire.
DeleteFile (ServerPath As String)
Deletes a file from the server.
The DeleteCompleted event will be raised when this task completes.
DownloadFileResume (ServerFilePath As String, AsciiFile As Boolean, DeviceFolder As String, DeviceFile As String, RestartOffset As Long)
Downloads or resumes down of a file from the server.
The DownloadCompleted event will be raised when download completes.
DownloadProgress events will be raised during download.
ServerFilePath - Full path to the remote file.
AsciiFile - If True then end of line characters will be converted as needed. Note that Android end of line character is the same as Unix / Linux.
DeviceFolder - Folder that the file will be saved to.
DeviceFile - The name of the local file that will be created.
RestartOffset - The first byte to request if resuming an incomplete download. Pass 0 if you wish to download the entire file.
Initialize (EventName As String, Host As String, Port As Int, User As String, Password As String)
Initializes the object and sets the subs that will handle the events
IsInitialized As Boolean
Tests whether the object was initialized.
List (ServerPath As String)
Fetches the list of folders and files in the specified path.
The ListCompleted event will be raised when the data is available.
PassiveMode As Boolean
Gets or sets whether FTP is in passive mode. The default mode is active mode.
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.
Common commands:
MKD - Creates a new folder.
RMD - Deletes an empty folder.
Example:
FTP.SendCommand("MKD", "/somefolder/newfolder")
SetCustomSSLTrustManager (TrustManager As Object)
UploadFileResume (DeviceFolder As String, DeviceFile As String, AsciiFile As Boolean, ServerFilePath As String, RestartOffset As Long)
Uploads a file to the server.
The UploadCompleted event will be raised when upload completes.
UploadProgress events will be raised during the upload.
DeviceFolder - Local folder.
DeviceFile - Local file name.
AsciiFile - If True then end of line characters will be converted as needed. Note that Android end of line character is the same as Unix / Linux.
ServerFilePath - Full path to file that will be created on the server.
RestartOffset - The first byte to send if resuming an incomplete upload. Pass 0 if you wish to upload the entire file.
UseSSL As Boolean
Gets or sets whether the connection should be done with SSL sockets (FTPS Implicit).
UseSSLExplicit As Boolean
Gets or sets whether the connection should be done with SSL sockets (FTPS Explicit).

SMTPExtras

SMTP object allows you to send emails with no user intervention and without relying on the device installed mail clients.
Both text messages and Html messages are supported as well as file attachments.
There are two encryption modes supported: UseSSL and StartTLSMode.
UseSSL means that the connection will be based on a SSL connection right from the start.
StartTLSMode means that the connection will only be upgraded to SSL after the client send the STARTTLS command. Most SMTP servers support this mode.
Gmail for example supports both modes. UseSSL on port 465 and StartTLSMode on port 587.

Example:
Sub Process_Globals
  Dim SMTP As SMTP
End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
    SMTP.Initialize("smtp.gmail.com", 587, "[email protected]", "mypassword", "SMTP")
    SMTP.StartTLSMode = True
  End If
  SMTP.To.Add("[email protected]")
  SMTP.Subject = "This is the subject"
  SMTP.Body = "This is the message body."
  SMTP.AddAttachment(File.DirRootExternal, "somefile")
  SMTP.Send
End Sub
Sub SMTP_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
End Sub

Permissions:

android.permission.INTERNET

Events:

MessageSent(Success As Boolean)
MessageSent2(Success As Boolean, Tag As Object)

Members:


  AddAttachment (Dir As String, FileName As String)

  AddHeader (HeaderField As String, HeaderValue As String)

  AUTH_CRAM_MD5 As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD

  AUTH_LOGIN As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD

  AUTH_PLAIN As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD

  AuthMethod As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD

  BCC As List

  Body As String

  CC As List

  DefaultTimeout As Int [write only]

  HtmlBody As Boolean

  Initialize (Server As String, Port As Int, Username As String, Password As String, EventName As String)

  Send

  Sender As String

  SoTimeout As Int [write only]

  StartTLSMode As Boolean

  Subject As String

  Tag As Object

  To As List

  UseSSL As Boolean

Members description:

AddAttachment (Dir As String, FileName As String)
Adds a file attachment.
AddHeader (HeaderField As String, HeaderValue As String)
Adds a header field and header value.
AUTH_CRAM_MD5 As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
AUTH_LOGIN As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
AUTH_PLAIN As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
AuthMethod As org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
Gets or sets the SMTP AUTH method. Default value is PLAIN.
BCC As List
Gets or sets the list of "BCC" recipients.
Body As String
Gets or sets the message body.
CC As List
Gets or sets the list of "CC" recipients.
DefaultTimeout As Int [write only]
Set the default timeout in milliseconds to use when opening a socket.
Default value is 60000.
HtmlBody As Boolean
Gets or sets whether this message body is Html text.
Initialize (Server As String, Port As Int, Username As String, Password As String, EventName As String)
Initializes the object.
Server - Server address. Host name or Ip.
Port - Mail server port.
Username - Account user name.
Password - Account password.
EventName - The name of the sub that will handle the MessageSent event.
Send
Sends the message. The MessageSent event will be raised after the message was sent.
Note that the message fields are cleared after this method to allow you to send new messages with the same object.
Sender As String
Gets or sets the Sender field. By default it is the same as the Username.
SoTimeout As Int [write only]
Set the timeout in milliseconds of a currently open socket connection.
Default value is 60000.
StartTLSMode As Boolean
Gets or sets whether the connection should be done in StartTLS mode.
Subject As String
Gets or sets the message subject.
Tag As Object
Gets or Sets a Tag object for the email task to be sent.
If a Tag object is set then the Send method will raise the event MessageSent2(Success As Boolean, Tag As Object) instead of the event MessageSent(Success As Boolean).
A unique Tag object can therefore be used to identify which email has been sent.
To As List
Gets or sets the list of "To" recipients.
Example:SMTP.To.Add("[email protected]")
UseSSL As Boolean
Gets or sets whether the connection should be done with SSL sockets.
Top