Skip to content

infinity.pop3

Module: infinity.pop3

Classes:
Namespaces:
Interfaces:

The infinity.pop3 namespace contains classes for accessing POP3 email servers as a client as well as building your own POP3 server.


Classes

client

You can use the infinity.pop3.client object to access a POP3 server and retrieve messages from it. The first thing you need to do is set the host, port and encryptMode properties to specify how to connect to the server. Then you'll usually need to set the authMode, username and password properties to specify how to authenticate yourself towards the server. You can also set the timeout to limit the time that the client will try to connect to the server.

After setting the properties, you can use the connect() method to establish a connection to the POP3 server, followed by calls to the other methods to list or retrieve messages. Make sure to call the free() method when finished, to release the memory allocated by the client object.

Example:

infinity.loadModule('infinity.pop3');
let myClient = new infinity.pop3.client();

try {
  myClient.host = 'pop.gmail.com';
  myClient.port = 995;
  myClient.encryptMode = infinity.pop3.client.encryptMode.sslTls;
  myClient.authMode = infinity.pop3.client.authMode.userPass;
  myClient.username = 'youraccount@gmail.com';
  myClient.password = 'yourpassword';

  myClient.connect();
  console.debug(myClient.list());
  console.debug(myClient.retrieve(1));
  myClient.disconnect();
} finally {
  myClient.free();
}

Properties


allowUnsafeLegacyRenegotiation

Type: boolean

Allows the deprecated ssl unsafe legacy renegotiation. This should be avoided and the server's ssl version should be fixed, instead. If that is not possible, then this option can be used to let the POP3 client establish a connection nonetheless. Default: false.


authMode

Type: infinity.pop3.client.authMode

Gets and sets the authorization mode for the POP3 client to use, according to infinity.pop3.client.authMode. Default: userPass.


encryptMode

Type: infinity.pop3.client.encryptMode

Gets and sets the encryption mode for the POP3 client to use according to infinity.pop3.client.encryptMode. Default: none.

With sslTls, the client uses implicit TLS if port is 995, and explicit TLS (STLS) on any other port. The server must offer STLS, otherwise connect() fails (since 3.11; before, the client went on unencrypted and sent the login data in plain text).


host

Type: string

Gets and sets the POP3 server address to connect to.


password

Type: string

Gets and sets the password for authorization.


port

Type: number

Gets and sets the communication port of the POP3 server. Default: 110.


timeout

Type: number

Gets and sets the timeout in milliseconds, used both for connecting and for reading server responses. Default: 5000.


useLegacyServerConnect

Type: boolean

Allows deprecated, unsafe ssl connections. This should be avoided and the server's ssl version should be fixed, instead. If that is not possible, then this option can be used to let the POP3 client establish a connection nonetheless. Default: false.


username

Type: string

Gets and sets the username for authorization.


verifyServerCertificate

Type: boolean

This option will turn on ssl certificate validation, which will result in a connection error if the server's ssl certificate is not valid (e.g. self-signed). Default: false.


Methods


constructor()

Lets you create an INFINITY.JS POP3 client object instance. Doesn't accept parameters.

Signature:

constructor()

Example:

infinity.loadModule('infinity.pop3');
let myClient = new infinity.pop3.client();

connect()

Connects to the POP3 server and logs in. The initial properties should be set by the time connect() is called.

Signature:

connect(): void

Example:

myClient.connect();

count()

Returns the number of messages in the mailbox on the POP3 server (POP3 STAT command). Messages marked for deletion in the current session are not counted.

Signature:

count(): number

Return type: number

Example:

let messageCount = myClient.count();

delete()

Marks a message on the POP3 server for deletion (POP3 DELE command). As specified in the POP3 protocol, the server removes the marked messages when the session is ended with disconnect() (which sends QUIT).

Signature:

delete( msgNum: number ): boolean

Parameters:

  • msgNum: number

    The number of the message to delete.

Return type: boolean

Returns true if the server accepted the command.

Example:

myClient.delete(1);

disconnect()

Ends the POP3 session by sending the QUIT command, which makes the server remove the messages marked for deletion.

Signature:

disconnect(): void

Example:

myClient.disconnect();

free()

Frees the memory previously occupied by the POP3 client object instance. An open connection is closed first.

Signature:

free(): void

Example:

myClient.free();

list()

Returns a infinity.pop3.listArray with the number and size of all messages on the POP3 server (POP3 LIST command). If a message number is specified, a single {msgNum, size} object for that message is returned instead of an array; an error is thrown if the message does not exist.

Signature:

list(): infinity.pop3.listArray
list( msgNum: number ): {msgNum: number, size: number}

Parameters:

  • msgNum: number, optional

    The number of the message for which to return list info. If unspecified, a list of all messages will be returned.

Return type: infinity.pop3.listArray or {msgNum: number, size: number}

Example:

let messageList = myClient.list();
console.debug(messageList[0]['size']);

retrieve()

Retrieves a raw (undecoded) message from the server. Without a filename the message is returned as a string, otherwise it is written to the file and nothing is returned. You can parse the message with infinity.mail.

Signature:

retrieve( msgNum: number ): string
retrieve( msgNum: number, filename: string ): void

Parameters:

  • msgNum: number

    The number of the message to retrieve.

  • filename: string, optional

    If a filename is specified, then the message will be saved to that file. The filename can be relative (location relative to the folder with the used INFINITY.JS executable file) or an absolute path.

Return type: string or void

Example:

let messageText = myClient.retrieve(1);
myClient.retrieve(1, 'message.eml');

size()

Returns the size of the specified message in bytes, or the total size of all messages in the mailbox if no message number is specified. Returns -1 if the size cannot be determined.

Signature:

size( msgNum?: number ): number

Parameters:

  • msgNum: number, optional

    The number of the message to examine. If unspecified, the total size of the mailbox is returned.

Return type: number

Example:

let messageSize = myClient.size(1);
let mailboxSize = myClient.size();

top()

Retrieves the headers and optionally the first lines of the body of the specified message (POP3 TOP command). Without a filename the result is returned as a string, otherwise it is written to the file and nothing is returned.

Signature:

top( msgNum: number, maxLines?: number ): string
top( msgNum: number, maxLines: number, filename: string, encoding?: infinity.encoding, append?: boolean, writeBom?: boolean ): void

Parameters:

  • msgNum: number

    The number of the message to retrieve.

  • maxLines: number, optional

    The number of body lines of the message to retrieve (in addition to the message headers). Default: 0 (headers only).

  • filename: string, optional

    If a filename is specified, then the result will be saved to that file. The filename can be relative (location relative to the folder with the used INFINITY.JS executable file) or an absolute path.

  • encoding: infinity.encoding, optional

    The encoding of the file. Default: infinity.encoding.utf8.

  • append: boolean, optional

    true to append to an existing file instead of overwriting it. Default: false.

  • writeBom: boolean, optional

    true to write a byte order mark to the file. Default: false.

Return type: string or void

Example:

let headers = myClient.top(1);
myClient.top(1, 10, 'top.txt');

uidl()

Returns a infinity.pop3.uidlArray with the message number and unique id of each message on the server (POP3 UIDL command). If a message number is specified, a single {msgNum, id} object for that message is returned instead of an array; an error is thrown if the message does not exist.

Signature:

uidl(): infinity.pop3.uidlArray
uidl( msgNum: number ): {msgNum: number, id: string}

Parameters:

  • msgNum: number, optional

    The number of the message. If unspecified, the ids of all messages are returned.

Return type: infinity.pop3.uidlArray or {msgNum: number, id: string}

Example:

let uidlList = myClient.uidl();

server

To run a POP3 server using infinity.pop3.server you need to set script filenames in the server's onConnect, onLogin, etc. properties. These script filenames have to be relative to the script that created the server (usually your main script; before 3.11 only files below the folder of the main script were found) and these scripts will be called when the POP3 server receives the corresponding POP3 command from a client. The script files are checked when start() is called; an error is thrown if one of them does not exist.

Within these scripts, you can access the infinity.pop3.session object, which will be filled with the parameters of the client's POP3 command. You will also use this POP3 session object to pass back your reply to the client. You can find the specifics per command in the documentation of each on... property.

The commands STAT, LIST, UIDL and RSET are answered by the server itself, based on the message items provided by the onLogin handler. Clients address messages by their message number, which is the position of the item in the items array (starting with 1).

Example:

infinity.loadModule('infinity.pop3');
let myServer = new infinity.pop3.server(true, false, 995);
myServer.onLogin = 'pop3/login.js';
myServer.onTop = 'pop3/top.js';
myServer.onRetrieve = 'pop3/retrieve.js';
myServer.onDelete = 'pop3/delete.js';
myServer.onQuit = 'pop3/quit.js';
myServer.start();

Properties


onConnect

Type: string

Script filename (relative to the main script) that will handle POP3 connection requests. Within that script, you can access the infinity.pop3.session object, which will contain the following properties:

  • id: POP3 session id
  • ip: IP address of the connecting POP3 client
  • port: port of the POP3 client connection
  • data: custom session data (see data)
  • disconnect: this is a function that you can call to disconnect the connection.

These properties will remain valid throughout the POP3 session, so you can access these in the other POP3 command handlers, as well. The only exception is the id property, which will become invalid in the onDisconnect handler.


onDelete

Type: string

Script filename (relative to the main script) that will handle POP3 delete requests. Within that script, you can access the infinity.pop3.session object, which will contain the same properties described in the onConnect handler, plus the following:

  • item: an object containing information about the message to be deleted, with the following properties:
  • deleted: the current deletion mark of the message (normally false). Set this to true to mark the item as deleted and thus signal success to the delete request; if it is false after the script ran, the client gets an error reply. Note that the delete request should not directly delete the message, but rather mark it for deletion (as specified in the POP3 protocol). The message will remain marked as deleted throughout the POP3 session (unless the client resets the delete flag) and you need to handle the onQuit request later to actually delete all the messages marked for deletion (as specified in the POP3 protocol).
  • id: the message's id
  • size: the messages size (in bytes)
  • uid: the message's uid

Deleting messages from POP3 works as follows: the client issues a delete request to the server, which will mark the message as deleted. The client can ask the server to reset all delete flags (which the infinity.pop3.server will automatically handle for you - you don't have to write your own command handler for that). When the client sends a quit command to the server, then the server is expected to delete all messages that have been marked as deleted. If the client disconnects without sending a quit command, then no messages should be deleted.


onDisconnect

Type: string

Script filename (relative to the main script) that will handle POP3 disconnects. Within that script, you can access the infinity.pop3.session object, which will contain the same properties described in the onConnect handler (except for the id property no longer being valid, because the connection is being disconnected), plus the following:

  • items: an array containing message items with the following properties:
  • deleted: true if the message has been marked as deleted, false otherwise
  • id: the message's id
  • size: the messages size (in bytes)
  • uid: the message's uid

Note: do not delete messages that have been marked as deleted in the onDisconnect handler. Messages should only be deleted in the onQuit handler (as specified in the POP3 protocol).


onLogin

Type: string

Script filename (relative to the main script) that will handle POP3 login requests. Within that script, you can access the infinity.pop3.session object, which will contain the same properties described in the onConnect handler, plus the following:

  • username: the username for authentication
  • password: the password for authentication
  • authenticated: false, set this to true if the credentials are valid
  • items: an empty array that you need to fill with information about the messages available:
  • deleted: true if the message is marked as deleted, false otherwise (optional, default false)
  • id: the message's id (a number)
  • size: the messages size (in bytes)
  • uid: the message's unique id, as reported to the client by the UIDL command (optional, default: the MD5 hash of id)

The username and password properties will remain valid throughout the rest of the POP3 session, in addition to the properties described in the onConnect command handler. You should validate these credentials and set the authenticated property accordingly. If authenticated is not true after the script ran (or no onLogin handler is set), the login is refused. If authentication is successful, you will also need to fill the items array and provide information about all the messages that can be retrieved. You can use arbitrary values for the id property - the POP3 client addresses messages by their position in the items array, and you will get the corresponding item (including its id) in the infinity.pop3.session.item object of the other command handlers.


onQuit

Type: string

Script filename (relative to the main script) that will handle POP3 quit requests. Within that script, you can access the infinity.pop3.session object, which will contain the same properties described in the onConnect handler, plus the following:

  • items: an array containing message items with the following properties:
  • deleted: true if the message should now be deleted, false if the message should not be deleted
  • id: the message's id
  • size: the messages size (in bytes)
  • uid: the message's uid

As specified in the POP3 protocol, all messages that have been marked as deleted should actually be deleted when the client sends the quit command. So you need to iterate through the infinity.pop3.session.items array and delete all messages from your storage that are marked as deleted.


onRetrieve

Type: string

Script filename (relative to the main script) that will handle POP3 retrieve requests. Within that script, you can access the infinity.pop3.session object, which will contain the same properties described in the onConnect handler, plus the following:

  • item: an object containing information about the message to be retrieved, with the following properties:
  • deleted: false (requests for messages marked for deletion are refused before the script is called)
  • id: the message's id
  • size: the messages size (in bytes)
  • uid: the message's uid
  • lines: 0 (not used for retrieve requests)
  • msg: an empty string, set this to the message data (as a string)

The message data (the infinity.pop3.session.item.msg property) should be a message according to the RFC822 standard. You can use infinity.mail to conveniently create RFC822 messages.


onTop

Type: string

Script filename (relative to the main script) that will handle POP3 top requests (requesting the headers and a certain number of lines from the top of the message content). Within that script, you can access the infinity.pop3.session object, which will contain the same properties described in the onConnect handler, plus the following:

  • item: an object containing information about the message to be retrieved, with the following properties:
  • deleted: false (requests for messages marked for deletion are refused before the script is called)
  • id: the message's id
  • size: the messages size (in bytes)
  • uid: the message's uid
  • lines: the number of lines of the message body that you should be returning (in addition to the message headers)
  • msg: an empty string, set this to the message data (as a string)

The message data (the infinity.pop3.session.item.msg property) should be a message according to the RFC822 standard. You can use infinity.mail to conveniently create RFC822 messages. In contrast to the retrieve command handled in onRetrieve, the top command asks the server to return only the message headers and a specified number of lines from the top of the message (hence the name of the command). Make sure to return only the message headers plus the top infinity.pop3.session.item.lines lines of the message content in the infinity.pop3.session.item.msg property.


Methods


constructor()

Creates a POP3 server object.

Signature:

constructor( ssl?: boolean, verifyCertificate?: boolean, port?: number, maxConnections?: number, poolSize?: number, minProtocol?: infinity.pop3.server.protocol, maxProtocol?: infinity.pop3.server.protocol )

Parameters:

  • ssl: boolean, optional

    true to accept encrypted connections (using TLS/SSL), false to only accept unencrypted connections (not recommended). Default: false. With ssl, the server initially uses the certificate files infinity.crt, infinity.key and infinity.root.crt from the resources folder next to the folder of the INFINITY.JS executable (if they exist); use setCertificate() to use your own certificate.

  • verifyCertificate: boolean, optional

    true to request and verify a client certificate (against the root certificate); connections from clients without a valid certificate are refused. Only effective if a root certificate file exists. Default: false.

  • port: number, optional

    The port on which to listen for POP3 client connections. Default: 995 if ssl is true, otherwise 110. With ssl, port 995 uses implicit SSL; on any other port the server accepts unencrypted connections and handles STLS requests by the client to switch to an encrypted connection.

  • maxConnections: number, optional

    Maximum number of simultaneous connections. Default: 0 (unlimited).

  • poolSize: number, optional

    Size of the thread pool. If greater than 0, the server keeps a pool of threads that are reused for new connections; otherwise a new thread is created for each connection. Default: 0.

  • minProtocol: infinity.pop3.server.protocol, optional

    Minimal encryption protocol accepted. You can use this to refuse client connections that are using old (possibly obsolete) encryption protocols. Default: SSL 3.

  • maxProtocol: infinity.pop3.server.protocol, optional

    Maximum encryption protocol accepted. Default: tls1_3.

Example:

infinity.loadModule('infinity.pop3');
let myServer = new infinity.pop3.server();
let mySslServer = new infinity.pop3.server(true, false, 995, 0, 0, infinity.pop3.server.protocol.tls1_2);

setCertificate()

Sets the SSL certificate for the current server instance using the corresponding files provided in the function parameters. Throws an error if the server was created without ssl. Files that do not exist are ignored.

Signature:

setCertificate( certFile: string, keyFile: string, rootCertFile?: string, verify?: boolean ): void

Parameters:

  • certFile: string

    The path to the certificate file to be used.

  • keyFile: string

    The path to the key file to be used.

  • rootCertFile: string, optional

    The path to the root certificate file to be used.

  • verify: boolean, optional

    true to request and verify client certificates against the root certificate (only effective if rootCertFile is given). Default: false.

Example:

myServer.setCertificate('certFile.crt', 'keyFile.key', 'rootCertFile.crt', true);

start()

Starts the POP3 server. The server will run as a separate thread, listening to incoming POP3 client connections and running the scripts specified in the on... command handlers.

Signature:

start(): void

Example:

myServer.start();

stop()

Stops the POP3 server. The server will close its port and stop listening for POP3 connections.

Signature:

stop(): void

Example:

myServer.stop();

Namespaces

infinity.pop3.client


Enums

infinity.pop3.client.authMode

Values:

  • userPass: 0

    Authenticate using username and password.

  • apop: 1

    Authenticate using APOP (Authenticated Post Office Protocol).

  • sasl: 2

    Authenticate using SASL (Simple Authentication and Security Layer). The mechanisms CRAM-SHA1, CRAM-MD5, PLAIN and LOGIN are offered in this order; the first one the server supports is used (since 3.12; before, SASL login always failed).

  • xoauth2: 3

    Authenticate with an OAuth 2.0 access token (SASL mechanism XOAUTH2), as required by Microsoft 365 / Exchange Online / Outlook.com and Google (Gmail, Google Workspace). Set username to the mailbox address and password to the access token; getting and renewing the token (OAuth authorization code flow) is up to the script. Only XOAUTH2 is used, there is no fallback to other mechanisms. If the server rejects the token, the exception contains the server's answer (e.g. Invalid credentials), so the script can renew the token and connect again. Since 3.20.

Example:

infinity.loadModule('infinity.pop3');
let authMode = infinity.pop3.client.authMode.userPass;

infinity.pop3.client.encryptMode

Values:

  • none: 0

    Unencrypted connection.

  • sslTls: 1

    SSL/TLS encrypted connection.

Example:

infinity.loadModule('infinity.pop3');
let encryptMode = infinity.pop3.client.encryptMode.sslTls;

infinity.pop3.server

Enums:

Enums

infinity.pop3.server.protocol

Values:

  • tls1: 2

    TLS 1.0 encrypted connection.

  • tls1_1: 3

    TLS 1.1 encrypted connection.

  • tls1_2: 4

    TLS 1.2 encrypted connection.

  • tls1_3: 5

    TLS 1.3 encrypted connection.

Example:

infinity.loadModule('infinity.pop3');
let protocol = infinity.pop3.server.protocol.tls1_2;

infinity.pop3.session

The infinity.pop3.session object will be available in the command handler scripts run by the infinity.pop3.server. The properties will be filled depending on the command handler and some of the properties are used to provide results of the handler to be returned back to the client.


Properties

authenticated

Type: boolean

Only available in the onLogin command handler. You need to set this property to true or false depending on whether the provided credentials (username and password) are valid.


data

Type: any

Custom data that you can keep for the session. The value is stored as JSON, so it must be JSON-serializable. Changes made in the onLogin, onRetrieve, onTop and onDelete handlers are kept and are available in all later handlers of the same session; changes made in onConnect, onDisconnect and onQuit are discarded. Initially undefined.


item

Type: {id: number, uid: string, size: number, deleted: boolean, msg: string, lines: number}

This property will be filled in the onRetrieve, onTop and onDelete command handlers, which deal with a specific message item (msg and lines only in onRetrieve and onTop). The properties of this object are:

  • id: the message id
  • uid: the message uid
  • size: the size of the message (in bytes/octets)
  • deleted: true if the message has been marked to be deleted, false otherwise
  • msg: you need to set this property in the onRetrieve and onTop command handlers. This needs to be a string containing a message in the RFC822 format. You can use infinity.mail to create messages and convert them to a RFC822 string.
  • lines: provided in the onTop command handler. This is the requested number of lines from the message's content requested by the client.

items

Type: infinity.pop3.itemArray

This is the current list of messages available in this POP3 session. In the onLogin handler it is empty and needs to be filled; in the onConnect, onDisconnect and onQuit handlers it contains the items provided at login, including their current deleted marks.


id

Type: number

The POP3 session id. This will remain constant throughout a single POP3 session and will turn invalid on disconnect.


ip

Type: string

The IP address from which the POP3 client connected.


password

Type: string

The password used to authenticate the POP3 session.


port

Type: number

The port used by the POP3 connection with the client.


username

Type: string

The username used to authenticate the POP3 session.


Functions

disconnect()

Disconnects the current POP3 connection.

Signature:

disconnect(): void


Interfaces

itemArray

Extends: Array<{id: number, uid: string, size: number, deleted: boolean}>

An array of POP3 message items.


listArray

Extends: Array<{msgNum: number, size: number}>

An array of POP3 message item id and size information.


uidlArray

Extends: Array<{msgNum: number, id: string}>

An array of POP3 message id and uid information.