Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mailman
oleobject loo_Email
oleobject loo_BdMime
oleobject loo_DistList
oleobject loo_SbRecipients
integer i
integer li_SzDistList
integer j

li_Success = 0

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

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

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

// Create a new email object
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Email.Subject = "This is a test"
loo_Email.Body = "This is a test"
loo_Email.From = "Senders Name <sender@example.com>"
loo_Email.AddTo("Subscribers","subscribers@example.com")

loo_BdMime = create oleobject
li_rc = loo_BdMime.ConnectToNewObject("Chilkat.BinData")

loo_Mailman.RenderToMimeBd(loo_Email,loo_BdMime)

// Load a file containing one email address per line.
loo_DistList = create oleobject
li_rc = loo_DistList.ConnectToNewObject("Chilkat.StringTable")

li_Success = loo_DistList.AppendFromFile(1000,"utf-8","qa_data/distList.txt")
if li_Success = 0 then
    Write-Debug loo_DistList.LastErrorText
    destroy loo_Mailman
    destroy loo_Email
    destroy loo_BdMime
    destroy loo_DistList
    return
end if

loo_SbRecipients = create oleobject
li_rc = loo_SbRecipients.ConnectToNewObject("Chilkat.StringBuilder")

i = 0
li_SzDistList = loo_DistList.Count
j = 0
do while i < li_SzDistList

    // Build a list of comma-separated recipients.
    if j > 0 then
        loo_SbRecipients.Append(",")
    end if

    loo_SbRecipients.Append(loo_DistList.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 = li_SzDistList) then
        li_Success = loo_Mailman.SendMimeBd("sender@example.com",loo_SbRecipients.GetAsString(),loo_BdMime)
        if li_Success <> 1 then
            Write-Debug loo_Mailman.LastErrorText
            destroy loo_Mailman
            destroy loo_Email
            destroy loo_BdMime
            destroy loo_DistList
            destroy loo_SbRecipients
            return
        end if

        j = 0
        loo_SbRecipients.Clear()
    end if

loop

li_Success = loo_Mailman.CloseSmtpConnection()
if li_Success <> 1 then
    Write-Debug "Connection to SMTP server not closed cleanly."
end if

Write-Debug "Email sent to distirbution list."


destroy loo_Mailman
destroy loo_Email
destroy loo_BdMime
destroy loo_DistList
destroy loo_SbRecipients