Sample code for 30+ languages & platforms
Xbase++

Send Email to Distribution List

See more SMTP Examples

Sends the same email to a list of 1000 email addresses in 50 sends where each email has 20 recipients.

Note: Chilkat is not intended nor designed for mass emailing. A solution such as this might be used for a corporate emailing to employees, or an emailing to newsletter subscribers.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMailman
LOCAL oEmail
LOCAL oBdMime
LOCAL oDistList
LOCAL oSbRecipients
LOCAL i
LOCAL nSzDistList
LOCAL j

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oMailman := CreateObject("Chilkat.MailMan")

oMailman:SmtpHost := "smtp.mymailserver.com"
oMailman:SmtpPort := 465
oMailman:SmtpSsl := 1
oMailman:SmtpUsername := "myUsername"
oMailman:SmtpPassword := "myPassword"

//  Create a new email object
oEmail := CreateObject("Chilkat.Email")
oEmail:Subject := "This is a test"
oEmail:Body := "This is a test"
oEmail:From := "Senders Name <sender@example.com>"
oEmail:AddTo("Subscribers", "subscribers@example.com")

oBdMime := CreateObject("Chilkat.BinData")
oMailman:RenderToMimeBd(oEmail, oBdMime)

//  Load a file containing one email address per line.
oDistList := CreateObject("Chilkat.StringTable")
nSuccess := oDistList:AppendFromFile(1000, "utf-8", "qa_data/distList.txt")
IF (nSuccess == 0)
    ? oDistList:LastErrorText
    oMailman:destroy()
    oEmail:destroy()
    oBdMime:destroy()
    oDistList:destroy()
    RETURN
ENDIF

oSbRecipients := CreateObject("Chilkat.StringBuilder")

i := 0
nSzDistList := oDistList:Count
j := 0
DO WHILE i < nSzDistList

    //  Build a list of comma-separated recipients.
    IF (j > 0)
        oSbRecipients:Append(",")
    ENDIF

    oSbRecipients:Append(oDistList:StringAt(i))

    i := i + 1
    j := j + 1

    //  If we have 20 recipients, or we have the final recipient in the final chunk, then send.
    IF ((j == 20) .OR. (i == nSzDistList))
        nSuccess := oMailman:SendMimeBd("sender@example.com", oSbRecipients:GetAsString(), oBdMime)
        IF (nSuccess != 1)
            ? oMailman:LastErrorText
            oMailman:destroy()
            oEmail:destroy()
            oBdMime:destroy()
            oDistList:destroy()
            oSbRecipients:destroy()
            RETURN
        ENDIF

        j := 0
        oSbRecipients:Clear()
    ENDIF

ENDDO

nSuccess := oMailman:CloseSmtpConnection()
IF (nSuccess != 1)
    ? "Connection to SMTP server not closed cleanly."
ENDIF

? "Email sent to distirbution list."

oMailman:destroy()
oEmail:destroy()
oBdMime:destroy()
oDistList:destroy()
oSbRecipients:destroy()