Sample code for 30+ languages & platforms
JavaScript

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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var mailman = new CkMailMan();

//  Configure the SMTP server connection.
mailman.SmtpHost = "smtp.example.com";
mailman.SmtpPort = 465;
mailman.SmtpSsl = true;
mailman.SmtpUsername = "user@example.com";
mailman.SmtpPassword = "myPassword";

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

success = mailman.OpenSmtpConnection();
if (success == false) {
    console.log(mailman.LastErrorText);
    return true;
}

//  ... send emails here ...

//  Explicitly close the SMTP connection (sends QUIT first).
success = mailman.CloseSmtpConnection();
if (success == false) {
    console.log(mailman.LastErrorText);
    return true;
}

console.log("SMTP connection closed.");