Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
string ls_Mailbox
oleobject loo_Mailman
oleobject loo_Email

li_Success = 0

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

ls_Mailbox = "user@example.com"
loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")
if li_rc < 0 then
    destroy loo_Mailman
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Mailman.SmtpHost = "smtp.gmail.com"
loo_Mailman.SmtpPort = 587
loo_Mailman.SmtpSsl = 0
loo_Mailman.StartTLS = 1
loo_Mailman.RequireSslCertVerify = 1
//  Alternative: port 465 with SmtpSsl=true and StartTLS=false (implicit TLS).
loo_Mailman.SmtpUsername = ls_Mailbox
//  Load the app password from protected configuration.
loo_Mailman.SmtpPassword = "YOUR_16_CHARACTER_APP_PASSWORD"

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Email.From = ls_Mailbox
loo_Email.Subject = "Sample message"
loo_Email.Body = "This message was sent through Gmail SMTP using an app password."
li_Success = loo_Email.AddTo("Recipient","recipient@example.net")
if li_Success <> 1 then
    Write-Debug "Could not add the recipient."
    destroy loo_Mailman
    destroy loo_Email
    return
end if

//  This sends a real message. Replace the recipient with an address you control.
li_Success = loo_Mailman.SendEmail(loo_Email)
if li_Success <> 1 then
    loo_Mailman.CloseSmtpConnection()
    Write-Debug "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.
    destroy loo_Mailman
    destroy loo_Email
    return
end if

li_Success = loo_Mailman.CloseSmtpConnection()
if li_Success <> 1 then
    Write-Debug "Gmail accepted the message, but closing the connection failed. Do not resend it."
    destroy loo_Mailman
    destroy loo_Email
    return
end if

Write-Debug "Gmail accepted the message for delivery."


destroy loo_Mailman
destroy loo_Email