Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman

li_Success = 0

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

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

//  Configure the SMTP server connection.
loo_Mailman.SmtpHost = "smtp.example.com"
loo_Mailman.SmtpPort = 465
loo_Mailman.SmtpSsl = 1
loo_Mailman.SmtpUsername = "user@example.com"
loo_Mailman.SmtpPassword = "myPassword"

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

li_Success = loo_Mailman.OpenSmtpConnection()
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    return
end if

//  ... send emails here ...

//  Explicitly close the SMTP connection (sends QUIT first).
li_Success = loo_Mailman.CloseSmtpConnection()
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    return
end if

Write-Debug "SMTP connection closed."


destroy loo_Mailman