Persits Software, Inc. Knowledge Base Articles

HOWTO: Sending messages to multiple recipients with AspEmail

Problem Description

The methods AddAddress, AddBcc, AddCC and AddReplyTo of AspEmail 4.5 (and all earlier versions) do not accept comma-separated lists of email addressed, therefore the following code snippet is incorrect:

' Incorrect for AspEmail 4.5 and earlier
Addr = "name1@company.com,name2@company.com,name3@company.com"
Mail.AddAddress Addr

Solution

To send email to multiple recipients, you must not use comma-separated lists of addresses. Instead, you must call the AddAddress (AddBCC, AddCC, AddReplyTo) method individually on each email address, as follows:

' Correct
Mail.AddAddress "name1@company.com"
Mail.AddAddress "name2@company.com"
Mail.AddAddress "name3@company.com"

If your application stores email addresses as comma-separated lists, you must extract individual addresses from it and pass them to AspEmail one by one.

The following code snippet extracts individual email addresses from a comma-separated string Addresses, and feeds them to AspEmail. The last address in the list is fed via the method AddAddress, the others via the method AddCC.

Set Mail = Server.CreateObject("Persits.MailSender")

Mail.Host = "mail.myisp.com"
Mail.From = "me@mycompany.com"
Mail.Subject = "Hello!"

Addresses = "name1@company.com,name2@company.com,name3@company.com"

Do
   NextComma = InStr(Addresses, ",")
   If NextComma = 0 Then
      Mail.AddAddress Trim(Addresses)
      Exit Do
   End If

   Address = Left(Addresses, NextComma - 1)
   Mail.AddCC Trim(Address)

   Addresses = Mid(Addresses, NextComma + 1)
Loop

Mail.Send