Gmail SMTP: Send with OAuth2 on Port 465
Send email through Gmail with implicit TLS on port 465 and OAuth2. Set SmtpSsl=true and StartTLS=false. This replaces the old ordinary-password example while preserving its URL.
This sends a real message. Set the From address to the authorized mailbox or a permitted send-as identity and use a recipient you control. SMTP acceptance does not guarantee delivery. Gmail sending limits still apply. After an uncertain submission, verify the outcome before retrying.
First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set token [new_CkJsonObject]
set success [CkJsonObject_LoadFile $token "gmail-mail-token.json"]
if {$success != 1} then {
puts "Could not read gmail-mail-token.json from the private working directory."
delete_CkJsonObject $token
exit
}
if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1] || [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
puts "An OAuth access token is required. Authorize or refresh the account first."
delete_CkJsonObject $token
exit
}
set mailbox "user@example.com"
set mailman [new_CkMailMan]
CkMailMan_put_SmtpHost $mailman "smtp.gmail.com"
CkMailMan_put_SmtpPort $mailman 465
CkMailMan_put_SmtpSsl $mailman 1
CkMailMan_put_StartTLS $mailman 0
CkMailMan_put_RequireSslCertVerify $mailman 1
# Alternative: port 587 with SmtpSsl=false and StartTLS=true.
CkMailMan_put_SmtpUsername $mailman $mailbox
CkMailMan_put_SmtpPassword $mailman ""
CkMailMan_put_OAuth2AccessToken $mailman [CkJsonObject_stringOf $token "access_token"]
set email [new_CkEmail]
CkEmail_put_From $email $mailbox
CkEmail_put_Subject $email "Sample message"
CkEmail_put_Body $email "This message was sent through Gmail SMTP using OAuth2."
set success [CkEmail_AddTo $email "Recipient" "recipient@example.net"]
if {$success != 1} then {
puts "Could not add the recipient."
delete_CkJsonObject $token
delete_CkMailMan $mailman
delete_CkEmail $email
exit
}
# This sends a real message. Replace the recipient with an address you control.
set success [CkMailMan_SendEmail $mailman $email]
if {$success != 1} then {
CkMailMan_CloseSmtpConnection $mailman
puts "SMTP send failed. Check the token, 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.
delete_CkJsonObject $token
delete_CkMailMan $mailman
delete_CkEmail $email
exit
}
set success [CkMailMan_CloseSmtpConnection $mailman]
if {$success != 1} then {
puts "Gmail accepted the message, but closing the connection failed. Do not resend it."
delete_CkJsonObject $token
delete_CkMailMan $mailman
delete_CkEmail $email
exit
}
puts "Gmail accepted the message for delivery."
delete_CkJsonObject $token
delete_CkMailMan $mailman
delete_CkEmail $email