Sample code for 30+ languages & platforms
Tcl

Close a Persistent SMTP Connection

See more SMTP Examples

Demonstrates the Chilkat MailMan.CloseSmtpConnection method, which explicitly closes the current SMTP connection. Before closing the socket, Chilkat sends the SMTP QUIT command so the server can end the session cleanly. This example opens a connection and then closes it.

Background: This is the companion to OpenSmtpConnection and completes the "open once, send many, close once" batch pattern. Sending QUIT before dropping the socket is proper SMTP etiquette: it tells the server you are finished so it can release resources gracefully rather than treating the disconnect as an abrupt drop.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the MailMan.CloseSmtpConnection method, which explicitly closes the current
#  SMTP connection.  Before closing the socket, Chilkat sends the SMTP QUIT command.

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"

#  Open a connection (which would be reused for multiple sends).

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

#  ... send emails here ...

#  Explicitly close the SMTP connection (sends QUIT first).
set success [CkMailMan_CloseSmtpConnection $mailman]
if {$success == 0} then {
    puts [CkMailMan_lastErrorText $mailman]
    delete_CkMailMan $mailman
    exit
}

puts "SMTP connection closed."

delete_CkMailMan $mailman