Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMailman
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    //  Configure the SMTP server connection.
    Set ComSmtpHost Of hoMailman To "smtp.example.com"
    Set ComSmtpPort Of hoMailman To 465
    Set ComSmtpSsl Of hoMailman To True
    Set ComSmtpUsername Of hoMailman To "user@example.com"
    Set ComSmtpPassword Of hoMailman To "myPassword"

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

    Get ComOpenSmtpConnection Of hoMailman To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  ... send emails here ...

    //  Explicitly close the SMTP connection (sends QUIT first).
    Get ComCloseSmtpConnection Of hoMailman To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "SMTP connection closed."


End_Procedure