Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMailman
LOCAL oBundle
LOCAL oEmail1
LOCAL oEmail2

nSuccess := 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.

oMailman := CreateObject("Chilkat.MailMan")

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

//  Build a bundle of emails to send.
oBundle := CreateObject("Chilkat.EmailBundle")

oEmail1 := CreateObject("Chilkat.Email")
oEmail1:Subject := "Message 1"
oEmail1:From := "alice@example.com"
oEmail1:AddTo("Bob", "bob@example.com")
oEmail1:Body := "This is the first message."
oBundle:AddEmail(oEmail1)

oEmail2 := CreateObject("Chilkat.Email")
oEmail2:Subject := "Message 2"
oEmail2:From := "alice@example.com"
oEmail2:AddTo("Carol", "carol@example.com")
oEmail2:Body := "This is the second message."
oBundle:AddEmail(oEmail2)

//  Send every email in the bundle.

nSuccess := oMailman:SendBundle(oBundle)
IF (nSuccess == 0)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oBundle:destroy()
    oEmail1:destroy()
    oEmail2:destroy()
    RETURN
ENDIF

? "Sent " + Str(oBundle:MessageCount) + " 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.

oMailman:destroy()
oBundle:destroy()
oEmail1:destroy()
oEmail2:destroy()