Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman

li_Success = 0

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

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"

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

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

Write-Debug "SMTP connection opened."

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

//  Close the connection when done.
li_Success = loo_Mailman.CloseSmtpConnection()
if li_Success = 0 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Mailman
    return
end if



destroy loo_Mailman