1.04
anywheresoftware.b4a.net.SMTPWrapper
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:<code>
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</code>
process
MessageSent(Success As Boolean)
android.permission.INTERNET
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.
void
ba
anywheresoftware.b4a.BA
Initialize
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.
void
Server
java.lang.String
Port
int
Username
java.lang.String
Password
java.lang.String
EventName
java.lang.String
AddAttachment
Adds a file attachment.
void
Dir
java.lang.String
FileName
java.lang.String
UseSSL
boolean
b
boolean
Gets or sets whether the connection should be done with SSL sockets.
BCC
anywheresoftware.b4a.objects.collections.List
BCC
anywheresoftware.b4a.objects.collections.List
Gets or sets the list of "BCC" recipients.
Body
java.lang.String
text
java.lang.String
Gets or sets the message body.
Subject
java.lang.String
text
java.lang.String
Gets or sets the message subject.
HtmlBody
boolean
b
boolean
Gets or sets whether this message body is Html text.
CC
anywheresoftware.b4a.objects.collections.List
CC
anywheresoftware.b4a.objects.collections.List
Gets or sets the list of "CC" recipients.
To
anywheresoftware.b4a.objects.collections.List
To
anywheresoftware.b4a.objects.collections.List
Gets or sets the list of "To" recipients.
Example:<code>SMTP.To.Add("email@example.com")</code>
AuthMethod
org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
m
org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
Gets or sets the SMTP AUTH method. Default value is PLAIN.
StartTLSMode
boolean
b
boolean
Gets or sets whether the connection should be done in StartTLS mode.
Sender
Gets or sets the Sender field. By default it is the same as the Username.
java.lang.String
AUTH_LOGIN
org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
AUTH_CRAM_MD5
org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
AUTH_PLAIN
org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD
anywheresoftware.b4a.net.POPWrapper
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:<code>
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</code>
process
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)
android.permission.INTERNET
Initialize
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.
void
Server
java.lang.String
Port
int
Username
java.lang.String
Password
java.lang.String
EventName
java.lang.String
Status
Gets the mailbox status. The StatusCompleted event will be raised when the request is completed with the number of messages and the total size.
void
ba
anywheresoftware.b4a.BA
Close
Closes the connection after all submitted tasks finish. Note that this method do not block.
void
DownloadMessageTop
Calls the server and downloads the top number of lines from the message. When the message is ready the DownloadedCompleted event is raised.
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.
void
ba
anywheresoftware.b4a.BA
MessageId
int
NumberOfLines
int
Delete
boolean
DownloadMessage
Calls the server and downloads a message. When the message is ready the DownloadedCompleted event is raised.
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.
void
ba
anywheresoftware.b4a.BA
MessageId
int
Delete
boolean
ListMessages
Calls the server and when data is ready raises the ListCompleted event.
See the example described above.
void
ba
anywheresoftware.b4a.BA
CloseNow
Closes the connection immediately without waiting for current tasks to finish.
void
IsInitialized
boolean
UseSSL
boolean
b
boolean
Gets or sets whether the connection should be done with SSL sockets.
anywheresoftware.b4a.net.FTPWrapper
FTP
FTP allows you to communicate with FTP servers.
See <link>FTP tutorial|http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/10407-android-ftp-tutorial.html</link> for more information.
process
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)
android.permission.INTERNET
Initialize
Initializes the object and sets the subs that will handle the events
void
ba
anywheresoftware.b4a.BA
EventName
java.lang.String
Host
java.lang.String
Port
int
User
java.lang.String
Password
java.lang.String
DeleteFile
Deletes a file from the server.
The DeleteCompleted event will be raised when this task completes.
void
ba
anywheresoftware.b4a.BA
ServerPath
java.lang.String
Close
Closes the connection after all submitted tasks finish. Note that this method does not block.
void
DownloadFile
Downloads 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.
void
ba
anywheresoftware.b4a.BA
ServerFilePath
java.lang.String
AsciiFile
boolean
DeviceFolder
java.lang.String
DeviceFile
java.lang.String
SetCustomSSLTrustManager
void
TrustManager
java.lang.Object
UploadFile
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.
void
ba
anywheresoftware.b4a.BA
DeviceFolder
java.lang.String
DeviceFile
java.lang.String
AsciiFile
boolean
ServerFilePath
java.lang.String
SendCommand
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:<code>
FTP.SendCommand("MKD", "/somefolder/newfolder")</code>
void
ba
anywheresoftware.b4a.BA
Command
java.lang.String
Parameters
java.lang.String
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.
void
List
Fetches the list of folders and files in the specified path.
The ListCompleted event will be raised when the data is available.
void
ba
anywheresoftware.b4a.BA
ServerPath
java.lang.String
IsInitialized
Tests whether the object was initialized.
boolean
UseSSL
boolean
b
boolean
Gets or sets whether the connection should be done with SSL sockets (FTPS Implicit).
PassiveMode
boolean
b
boolean
Gets or sets whether FTP is in passive mode. The default mode is active mode.
UseSSLExplicit
boolean
b
boolean
Gets or sets whether the connection should be done with SSL sockets (FTPS Explicit).
anywheresoftware.b4a.net.FTPWrapper.FTPFileWrapper
FTPEntry
FTPEntry represents a file or a folder. Call FTP.List to get the files and folders.
org.apache.commons.net.ftp.FTPFile
process
IsInitialized
boolean
Name
java.lang.String
Timestamp
long
Size
long
anywheresoftware.b4a.net.FTPWrapper.CustomTrustManager
CustomTrustManager
CustomTrustManager allows you to create a SSL trust manager from a cert file or to create a trust manager that accepts all certificates.
javax.net.ssl.TrustManager[]
process
Initialize
Initializes the trust manager based on the given cert file.
void
Dir
java.lang.String
FileName
java.lang.String
InitializeAcceptAll
Initializes an "accept all" trust manager. This option should only be used in safe networks as it offers no real protection.
void
IsInitialized
boolean
1.53
The Net library implements the following protocols: FTP, SMTP and POP3. Both regular connections and secured connections are supported.
The implementations are based on <link>Apache Commons Net|http://commons.apache.org/net/</link>.
All the methods in this library are non-blocking.
This library replaces the FTP library.