Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

Gmail SMTP: Send with an App Password on Port 587

See more SMTP Examples

Send through Gmail on port 587 using required STARTTLS and an app password. This page replaces the former less-secure password-authentication instructions. Port 465 uses implicit TLS instead.

This sends a real email. Use a recipient you control and the account's authorized sender identity. Gmail sending limits still apply. For OAuth2, use the token-based sending example.

Use an account-specific app password generated after enabling 2-Step Verification, loaded from protected configuration. A normal Google password will not work. App passwords may be unavailable under account or Workspace policy; OAuth2 is preferred. Changing the Google account password revokes app passwords. The obsolete less-secure-apps switch is not a setup option. Google app-password requirements.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sMailbox
    Handle hoMailman
    Variant vEmail
    Handle hoEmail

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    Move "user@example.com" To sMailbox
    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End
    Set ComSmtpHost Of hoMailman To "smtp.gmail.com"
    Set ComSmtpPort Of hoMailman To 587
    Set ComSmtpSsl Of hoMailman To False
    Set ComStartTLS Of hoMailman To True
    Set ComRequireSslCertVerify Of hoMailman To True
    //  Alternative: port 465 with SmtpSsl=true and StartTLS=false (implicit TLS).
    Set ComSmtpUsername Of hoMailman To sMailbox
    //  Load the app password from protected configuration.
    Set ComSmtpPassword Of hoMailman To "YOUR_16_CHARACTER_APP_PASSWORD"

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComFrom Of hoEmail To sMailbox
    Set ComSubject Of hoEmail To "Sample message"
    Set ComBody Of hoEmail To "This message was sent through Gmail SMTP using an app password."
    Get ComAddTo Of hoEmail "Recipient" "recipient@example.net" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not add the recipient."
        Procedure_Return
    End

    //  This sends a real message. Replace the recipient with an address you control.
    Get pvComObject of hoEmail to vEmail
    Get ComSendEmail Of hoMailman vEmail To iSuccess
    If (iSuccess <> True) Begin
        Get ComCloseSmtpConnection Of hoMailman To iSuccess
        Showln "SMTP send failed. Check the app password, mailbox, recipient, and Gmail sending limits."
        //  Inspect LastErrorText privately if needed; do not publish authentication/session logs.
        //  A lost connection after DATA can leave acceptance uncertain. Avoid blindly retrying.
        Procedure_Return
    End

    Get ComCloseSmtpConnection Of hoMailman To iSuccess
    If (iSuccess <> True) Begin
        Showln "Gmail accepted the message, but closing the connection failed. Do not resend it."
        Procedure_Return
    End

    Showln "Gmail accepted the message for delivery."


End_Procedure