OScript API/Built-in Package Index

Class: SMTPSession

The built-in functions in the SMTPSession class provide the ability to connect to an SMTP server with a variety of methods and send email and SMTP commands.

The SMTPSession class offers the following functionality:

Class Attributes

Login using clear text

Login using CRAM_MD5

Login using an OAuth2 token

The standard Secure SMTP port, 465

The standard SMTP port, 25

Class Methods

New( String host, [Integer port] )

Returns a new SMTPSession instance, or undefined if an error occured.

NewSSL( String host, Integer port, SSLOptions options )

Returns a new SMTPSession instance using a SSL socket, or undefined if an error occured.

Instance Methods

Close( )

Close a connection to an SMTP server.

Connect( String host )

Connect to an SMTP server.

Retrieve the string for the last error code.

Returns the SMTP timeout in milliseconds.

InitContext( SSLOptions options )

Used to initialize a security context for SecureLogin().

Login( String username, String password, [Integer loginMethod] )

Initiate a connection to an SMTP server.

SecureLogin( String hostname, [String username], [String password], [Integer loginMethod] )

Initiate a secure connection to an SMTP server.

SendCommand( String command, String arguments )

Sends raw SMTP commands.

SendMessage( MailMessage message, RecArray badAddresses )

Sends a mail message.

SetTimeout( Integer timeout )

Set SMTP timeout in milliseconds.

Class Attributes

Integer LOGIN_CLEAR

Login using clear text

Login using CRAM_MD5

Integer LOGIN_XOAUTH2

Login using an OAuth2 token

The standard Secure SMTP port, 465

Integer SMTP_PORT

The standard SMTP port, 25

Class Methods

New

SMTPSession New( String host,
                 [Integer port] )

Returns a new SMTPSession object

Parameters

host

IP address or domain name of SMTP host.

port

SMTP connection port; default value is 25.

Returns:

A new SMTPSession object or undefined if there was an error constructing the object.

NewSSL

SMTPSession NewSSL( String host,
                    Integer port,
                    SSLOptions options )

Returns a new SMTPSession object using a SSL socket.

Parameters

host

IP address or domain name of SMTP host.

port

SMTP connection port

options

SSL options

Returns:

A new SMTPSession object using a SSL socket or undefined if there was an error constructing the object.

Instance Methods

Close

Integer Close()

No parameters. Closes the session.

Returns:

Returns 0 for success and -1 if an error occurred.

Connect

Integer Connect( String host )

The host parameter is optional and if not specified, will connect to the host as specified in the New() function.
Otherwise, this host parameter will override whatever host param was used in the New() function.
Use this if you do not require authentication to connect to your smtp server.

Parameters

host

IP address or domain name of SMTP server.

Returns:

Returns 0 for success and -1 if an error occurred.

GetLastError

String GetLastError()

No parameters. Returns an error string for the last error that occured.

Returns:

Returns an error string for the last error that occured.

GetTimeout

Integer GetTimeout()

Gets the timeout value for SMTP commands. The timeout is in milliseconds.

Returns:

Returns the timeout in milliseconds, -1 if an error occured.

InitContext

Integer InitContext( SSLOptions options )

Sets the SSL options for a secure connection. You must call this before attempting to do a SecureLogin() call.

Parameters

options

an instance of SSLOptions.

Returns:

Returns 0 for success and -1 if an error occurred.

Login

Integer Login( String username,
               String password,
               [Integer loginMethod] )

This function allows authenticated login to an SMTP server, with three different methods available, LOGIN_CRAM_MD5, LOGIN_CLEAR, and LOGIN_XOAUTH2. The loginMethod param is optional and the default is LOGIN_CLEAR.

Parameters

username

username used to login to SMTP server.

password

user password or authentication token for SMTP server.

loginMethod

type of login authentication to use: one of LOGIN_CRAM_MD5, LOGIN_CLEAR, or LOGIN_XOAUTH2.

Returns:

Returns 0 for success and -1 if an error occurred.

SecureLogin

Integer SecureLogin( String hostname,
                     [String username],
                     [String password],
                     [Integer loginMethod] )

The username/password parameters are optional, use them if your server also requires authentication as well as TLS/SSL.
You must have called InitContext() before this.

Parameters

hostname

IP address or domain name of SMTP server.

username

user login on SMTP server.

password

user password or authentication token for SMTP server.

loginMethod

type of login authentication to use: one of LOGIN_CRAM_MD5, LOGIN_CLEAR, or LOGIN_XOAUTH2.

Returns:

Returns 0 for success and -1 if an error occurred.

SendCommand

Integer SendCommand( String command,
                     String arguments )

Use this to send other raw SMTP commands. The arguments param is optional.

Parameters

command

command to send

arguments

arguments to include with the command

Returns:

Returns 0 for success and -1 if an error occurred.

SendMessage

Integer SendMessage( MailMessage message,
                     RecArray badAddresses )

Sends a previously created MailMessage.

Parameters

message

MailMessage to send

badAddresses

The list of recipient addresses that were rejected from smtp server

Returns:

Returns 0 for success and -1 if an error occurred. For backwards compatibility, if the optional badAddresses argument is omitted, then if any one of the recipient addresses within the message is rejected by the SMTP server for the session, then an error has occurred, -1 is returned and the details (as with any error) can be obtained via GetLastError. If the RecArray badAddresses argument is provided, then, rather than rejecting the message upon the first rejection of a recipient address, the information regarding the rejected address is placed in a Record in badAddresses and the next recipient address is provided to the SMTP server. Provided at least one valid address is present, then the message will be sent to all valid addresses, and the information about the rejected addresses will be available to the calling function via the Record(s) in the badAddresses argument.
The columns of such a Record (which are also added to the RecArray if they do not already exist) are "address" [a String value containing the address that was rejected by the SMTP server], "status" [an Integer value containing the numeric code with which the address was rejected], and "response" [a String value containing the full text of the rejection]. Note that the "status" value is extracted from the "response" value for convenience.

SetTimeout

Integer SetTimeout( Integer timeout )

Sets the timeout value for SMTP commands. The timeout is in milliseconds.

Parameters

timeout

timeout in milliseconds.

Returns:

Returns 0 for success and -1 if an error occurred.

 Copyright © 2022 OpenText Corporation. All rights reserved.