Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

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

    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Configure the SMTP server connection.
    CkMailMan::setCkSmtpHost(mailman, "smtp.example.com")
    CkMailMan::setCkSmtpPort(mailman, 465)
    CkMailMan::setCkSmtpSsl(mailman, 1)
    CkMailMan::setCkSmtpUsername(mailman, "user@example.com")
    CkMailMan::setCkSmtpPassword(mailman, "myPassword")

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

    success = CkMailMan::ckOpenSmtpConnection(mailman)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    ;  ... send emails here ...

    ;  Explicitly close the SMTP connection (sends QUIT first).
    success = CkMailMan::ckCloseSmtpConnection(mailman)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    Debug "SMTP connection closed."


    CkMailMan::ckDispose(mailman)


    ProcedureReturn
EndProcedure