PureBasic
PureBasic
Send a Bundle of Emails via SMTP
See more SMTP Examples
Demonstrates the Chilkat MailMan.SendBundle method, which sends each email in an EmailBundle. This is equivalent to calling SendEmail once for each email in the bundle. This example builds a bundle of two messages and sends them.
Background: Sending a bundle lets Chilkat reuse a single SMTP connection for all the messages, avoiding the connect/authenticate overhead of sending them one at a time — the same efficiency as the explicit open/send-loop/close pattern, in one call. It is handy for dispatching a queued batch of distinct messages (as opposed to one message to many recipients).
Chilkat PureBasic Downloads
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the MailMan.SendBundle method, which sends each email in an EmailBundle.
; This is equivalent to calling SendEmail once for each email in the bundle.
mailman.i = CkMailMan::ckCreate()
If mailman.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Configure the SMTP server connection.
CkMailMan::setCkSmtpHost(mailman, "smtp.example.com")
CkMailMan::setCkSmtpPort(mailman, 465)
CkMailMan::setCkSmtpSsl(mailman, 1)
CkMailMan::setCkSmtpUsername(mailman, "user@example.com")
CkMailMan::setCkSmtpPassword(mailman, "myPassword")
; Build a bundle of emails to send.
bundle.i = CkEmailBundle::ckCreate()
If bundle.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
email1.i = CkEmail::ckCreate()
If email1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email1, "Message 1")
CkEmail::setCkFrom(email1, "alice@example.com")
CkEmail::ckAddTo(email1,"Bob","bob@example.com")
CkEmail::setCkBody(email1, "This is the first message.")
CkEmailBundle::ckAddEmail(bundle,email1)
email2.i = CkEmail::ckCreate()
If email2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkSubject(email2, "Message 2")
CkEmail::setCkFrom(email2, "alice@example.com")
CkEmail::ckAddTo(email2,"Carol","carol@example.com")
CkEmail::setCkBody(email2, "This is the second message.")
CkEmailBundle::ckAddEmail(bundle,email2)
; Send every email in the bundle.
success = CkMailMan::ckSendBundle(mailman,bundle)
If success = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
CkEmailBundle::ckDispose(bundle)
CkEmail::ckDispose(email1)
CkEmail::ckDispose(email2)
ProcedureReturn
EndIf
Debug "Sent " + Str(CkEmailBundle::ckMessageCount(bundle)) + " emails."
; Note: Explicitly connecting/authenticating is optional. Chilkat MailMan automatically
; connects and authenticates -- using the property settings above -- whenever a server
; operation requires it. Calling the explicit connect/authenticate methods can still be
; helpful to determine whether a failure occurs while connecting or while authenticating.
CkMailMan::ckDispose(mailman)
CkEmailBundle::ckDispose(bundle)
CkEmail::ckDispose(email1)
CkEmail::ckDispose(email2)
ProcedureReturn
EndProcedure