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

jNet

The Net library implements the following protocols: FTP, SMTP and POP3. Both regular connections and secured connections are supported.
The implementations are based on
Apache Commons Net.
All the methods in this library are non-blocking.
This library replaces the FTP library.

List of types:

CustomTrustManager
FTP
FTPEntry
POP3
SMTP

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

FTP

FTP allows you to communicate with FTP servers.

Events:

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

Members:


  AppendFile (DeviceFolder As String, DeviceFile As String, AsciiFile As Boolean, ServerFilePath As String) As Object

  Close

  CloseNow

  DeleteFile (ServerPath As String) As Object

  DownloadFile (ServerFilePath As String, AsciiFile As Boolean, DeviceFolder As String, DeviceFile As String) As Object

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

  IsInitialized As Boolean

  List (ServerPath As String) As Object

  PassiveMode As Boolean

  SendCommand (Command As String, Parameters As String) As Object

  SetCustomSSLTrustManager (TrustManager As Object)

  TimeoutMs As Int

  UploadFile (DeviceFolder As String, DeviceFile As String, AsciiFile As Boolean, ServerFilePath As String) As Object

  UseSSL As Boolean

  UseSSLExplicit As Boolean

Members description:

AppendFile (DeviceFolder As String, DeviceFile As String, AsciiFile As Boolean, ServerFilePath As String) As Object
Similar to UploadFile. Appends the data to an existing file (if such exists).
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) As Object
Deletes a file from the server.
The DeleteCompleted event will be raised when this task completes.
Returns an object that can be used as the sender filter parameter in a Wait For call.
DownloadFile (ServerFilePath As String, AsciiFile As Boolean, DeviceFolder As String, DeviceFile As String) As Object
Downloads a file from the server. The DownloadCompleted event will be raised when download completes.
Returns an object that can be used as the sender filter parameter in a Wait For call.
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.
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) As Object
Fetches the list of folders and files in the specified path.
The ListCompleted event will be raised when the data is available.
Returns an object that can be used as the sender filter parameter in a Wait For call.
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) As Object
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.
Returns an object that can be used as the sender filter parameter in a Wait For call.

Common commands:
MKD - Creates a new folder.
RMD - Deletes an empty folder.
Example:
FTP.SendCommand("MKD", "/somefolder/newfolder")
SetCustomSSLTrustManager (TrustManager As Object)
TimeoutMs As Int
Communication timeout in milliseconds. The default value is 60000 (60 seconds).
UploadFile (DeviceFolder As String, DeviceFile As String, AsciiFile As Boolean, ServerFilePath As String) As Object
Uploads a file to the server. The UploadCompleted event will be raised when upload completes.
Returns an object that can be used as the sender filter parameter in a Wait For call.
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.
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).

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]

POP3

POP3 object allows you to connect to mail servers and read the mail messages.
This object returns the raw string of each message, including the headers. Parsing the raw string is currently out of the scope of this library.
The connection is established when it is first required.
ListCompleted event passes a parameter named Messages. This is a map with the messages IDs as keys and the messages sizes as values.
DownloadCompleted event passes the message raw string in the Message parameter.
Example:
Sub Process_Globals
  Dim POP As POP3
End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
    POP.Initialize("pop.gmail.com", 995, "example@gmail.com", "mypassword", "pop")
    POP.UseSSL = True 'Gmail requires SSL.
  End If
  POP.ListMessages
End Sub

Sub POP_ListCompleted (Success As Boolean, Messages As Map)
  Log("List: " & Success)
  If Success Then
    For i = 0 To Messages.Size - 1
      Pop.DownloadMessage(Messages.GetKeyAt(i), True) 'Download all messages and delete them
    Next
  Else
    Log(LastException.Message)
  End If
  POP.Close 'The connection will be closed after all messages are downloaded
End Sub
Sub POP_DownloadCompleted (Success As Boolean, MessageId As Int, Message As String)
  Log("Download: " & Success & ", " & MessageId)
  If Success Then
    Log(Message)
    Log(Message.Length)
    Log(MessageId)
  Else
    Log(LastException.Message)
  End If
End Sub

Events:

ListCompleted (Success As Boolean, Messages As Map)
DownloadCompleted (Success As Boolean, MessageId As Int, Message As String)
StatusCompleted (Success As Boolean, NumberOfMessages As Int, TotalSize As Int)

Members:


  Close

  CloseNow

  DownloadMessage (MessageId As Int, Delete As Boolean) As Object

  DownloadMessageTop (MessageId As Int, NumberOfLines As Int, Delete As Boolean) As Object

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

  IsInitialized As Boolean

  ListMessages As Object

  SetCustomSSLTrustManager (TrustManager As Object)

  Status As Object

  UseSSL 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.
DownloadMessage (MessageId As Int, Delete As Boolean) As Object
Calls the server and downloads a message. When the message is ready the DownloadedCompleted event is raised.
Returns an object that can be used as the sender filter parameter in a Wait For call.
MessageId - The message id which was previously retrieved by calling ListMessages.
Delete - Whether to delete the message after it is downloaded. Note that the message will only be deleted after the connection is closed.
DownloadMessageTop (MessageId As Int, NumberOfLines As Int, Delete As Boolean) As Object
Calls the server and downloads the top number of lines from the message. When the message is ready the DownloadedCompleted event is raised.
Returns an object that can be used as the sender filter parameter in a Wait For call.
MessageId - The message id which was previously retrieved by calling ListMessages.
NumberOfLines - Maximum number of lines to read from the message.
Delete - Whether to delete the message after it is downloaded. Note that the message will only be deleted after the connection is closed.
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.
IsInitialized As Boolean
ListMessages As Object
Calls the server and when data is ready raises the ListCompleted event.
Returns an object that can be used as the sender filter parameter in a Wait For call.
See the example described above.
SetCustomSSLTrustManager (TrustManager As Object)
Status As Object
Gets the mailbox status. The StatusCompleted event will be raised when the request is completed with the number of messages and the total size.
Returns an object that can be used as the sender filter parameter in a Wait For call.
UseSSL As Boolean
Gets or sets whether the connection should be done with SSL sockets.

SMTP

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, "example@gmail.com", "mypassword", "SMTP")
    SMTP.StartTLSMode = True
  End If
  SMTP.To.Add("othermail@example.com")
  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

Events:

MessageSent(Success As Boolean)

Members:


  AddAttachment (Dir As String, FileName As String)

  AdditionalHeaders As Map

  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

  HtmlBody As Boolean

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

  MailFrom As String

  Send As Object

  Sender As String

  SetCustomSSLTrustManager (TrustManager As Object)

  StartTLSMode As Boolean

  Subject As String

  To As List

  UseSSL As Boolean

Members description:

AddAttachment (Dir As String, FileName As String)
Adds a file attachment.
AdditionalHeaders As Map
Additional headers that will be added to the message.
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.
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.
MailFrom As String
Gets or sets the mail address that is sent with the MAIL command. By default it is the same as the Username.
Send As Object
Sends the message. The MessageSent event will be raised after the message was sent.
Returns an object that can be used as the sender filter parameter in a Wait For call.
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 header. By default it is the same as the Username.
SetCustomSSLTrustManager (TrustManager As Object)
StartTLSMode As Boolean
Gets or sets whether the connection should be done in StartTLS mode.
Subject As String
Gets or sets the message subject.
To As List
Gets or sets the list of "To" recipients.
Example:SMTP.To.Add("email@example.com")
UseSSL As Boolean
Gets or sets whether the connection should be done with SSL sockets.
Top