Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  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.

set mailman [new_CkMailMan]

#  Configure the SMTP server connection.
CkMailMan_put_SmtpHost $mailman "smtp.example.com"
CkMailMan_put_SmtpPort $mailman 465
CkMailMan_put_SmtpSsl $mailman 1
CkMailMan_put_SmtpUsername $mailman "user@example.com"
CkMailMan_put_SmtpPassword $mailman "myPassword"

#  Explicitly open (and authenticate) the SMTP connection.

set success [CkMailMan_OpenSmtpConnection $mailman]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    exit
}

puts "SMTP connection opened."

#  ... send one or more emails here ...

#  Close the connection when done.
set success [CkMailMan_CloseSmtpConnection $mailman]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    exit
}


delete_CkMailMan $mailman