Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringTable.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

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

    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkMailMan::setCkSmtpHost(mailman, "smtp.mymailserver.com")
    CkMailMan::setCkSmtpPort(mailman, 465)
    CkMailMan::setCkSmtpSsl(mailman, 1)
    CkMailMan::setCkSmtpUsername(mailman, "myUsername")
    CkMailMan::setCkSmtpPassword(mailman, "myPassword")

    ; Create a new email object
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "This is a test")
    CkEmail::setCkBody(email, "This is a test")
    CkEmail::setCkFrom(email, "Senders Name <sender@example.com>")
    CkEmail::ckAddTo(email,"Subscribers","subscribers@example.com")

    bdMime.i = CkBinData::ckCreate()
    If bdMime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkMailMan::ckRenderToMimeBd(mailman,email,bdMime)

    ; Load a file containing one email address per line.
    distList.i = CkStringTable::ckCreate()
    If distList.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkStringTable::ckAppendFromFile(distList,1000,"utf-8","qa_data/distList.txt")
    If success = 0
        Debug CkStringTable::ckLastErrorText(distList)
        CkMailMan::ckDispose(mailman)
        CkEmail::ckDispose(email)
        CkBinData::ckDispose(bdMime)
        CkStringTable::ckDispose(distList)
        ProcedureReturn
    EndIf

    sbRecipients.i = CkStringBuilder::ckCreate()
    If sbRecipients.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    szDistList.i = CkStringTable::ckCount(distList)
    j.i = 0
    While i < szDistList

        ; Build a list of comma-separated recipients.
        If j > 0
            CkStringBuilder::ckAppend(sbRecipients,",")
        EndIf

        CkStringBuilder::ckAppend(sbRecipients,CkStringTable::ckStringAt(distList,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 = szDistList)
            success = CkMailMan::ckSendMimeBd(mailman,"sender@example.com",CkStringBuilder::ckGetAsString(sbRecipients),bdMime)
            If success <> 1
                Debug CkMailMan::ckLastErrorText(mailman)
                CkMailMan::ckDispose(mailman)
                CkEmail::ckDispose(email)
                CkBinData::ckDispose(bdMime)
                CkStringTable::ckDispose(distList)
                CkStringBuilder::ckDispose(sbRecipients)
                ProcedureReturn
            EndIf

            j = 0
            CkStringBuilder::ckClear(sbRecipients)
        EndIf

    Wend

    success = CkMailMan::ckCloseSmtpConnection(mailman)
    If success <> 1
        Debug "Connection to SMTP server not closed cleanly."
    EndIf

    Debug "Email sent to distirbution list."


    CkMailMan::ckDispose(mailman)
    CkEmail::ckDispose(email)
    CkBinData::ckDispose(bdMime)
    CkStringTable::ckDispose(distList)
    CkStringBuilder::ckDispose(sbRecipients)


    ProcedureReturn
EndProcedure