Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMailman
    String sTemp1

    Move False To iSuccess

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

    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"

    //  Explicitly open (and authenticate) the SMTP connection.

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

    Showln "SMTP connection opened."

    //  ... send one or more emails here ...

    //  Close the connection when done.
    Get ComCloseSmtpConnection Of hoMailman To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMailman To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure