Sample code for 30+ languages & platforms
Lua

Open a Persistent SMTP Connection

See more SMTP Examples

Demonstrates the Chilkat MailMan.OpenSmtpConnection method, which explicitly opens a connection to the SMTP server and authenticates if a username and password, OAuth2 token, or other applicable settings have been provided. This example opens the connection, notes where sending would occur, and closes it.

Background: By default, sending a single email opens a connection, sends, and closes it. When sending many messages, that per-message overhead adds up. Explicitly calling OpenSmtpConnection once, sending in a loop, then CloseSmtpConnection keeps a single authenticated session open for the whole batch — substantially faster and gentler on the server than reconnecting each time.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the MailMan.OpenSmtpConnection method, which explicitly opens a connection
-- to the SMTP server and authenticates if credentials (or an OAuth2 token) have been
-- provided.

local mailman = chilkat.MailMan.new()

-- Configure the SMTP server connection.
mailman.smtp_host = "smtp.example.com"
mailman.smtp_port = 465
mailman.smtp_ssl = true
mailman.smtp_username = "user@example.com"
mailman.smtp_password = "myPassword"

-- Explicitly open (and authenticate) the SMTP connection.

local success = mailman:open_smtp_connection()
if not success then
    print(mailman.last_error_text)
    return
end

print("SMTP connection opened.")

-- ... send one or more emails here ...

-- Close the connection when done.
success = mailman:close_smtp_connection()
if not success then
    print(mailman.last_error_text)
    return
end