I have an asp.net 4 application in which I have created a form to create an email that gets sent to multiple recipients as BCC. In the form I show a multi-select box from which a user can select all or some of the email addresses that should receive the message. The form works fine, but only sends an email to the first email in the dataset. The databse is a small access db. How do I get either a concatenated list of all recipient emails or loop through the BCC code behind below: Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage() mailMessage.From = New System.Net.Mail.MailAddress(emailFromParents.Text.Trim()) mailMessage.To.Add(New System.Net.Mail.MailAddress(emailToParents.Text.Trim())) mailMessage.CC.Add(New System.Net.Mail.MailAddress(emailCCParents.Text.Trim())) mailMessage.Bcc.Add(New System.Net.Mail.MailAddress(emailParents.Text.Trim())) mailMessage.ReplyToList.Add(New System.Net.Mail.MailAddress(emailReplyToParents.Text.Trim())) mailMessage.Subject = emailSubjectParents.Text.Trim() mailMessage.Body = emailBodyParents.Text.Trim() Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient() The aspx code for the form that invokes this codebehind is pasted below: <asp:ListBox ID="emailParents" runat="server" AppendDataBoundItems="True" DataSourceID="ParentsEmail" DataTextField="ParentEMail" DataValueField="ParentEMail" Height="200px" SelectionMode="Multiple" Width="300px"> <asp:ListItem Selected="True">Select Parent E-Mails</asp:ListItem> </asp:ListBox> <asp:AccessDataSource ID="ParentsEmail" runat="server" DataFile="~/App_Data/CHSMarchingBand.accdb" SelectCommand="SELECT DISTINCT ParentEMail FROM MarchingBandStudents WHERE (Active = Yes)"> </asp:AccessDataSource>
I'm not a Visual Basic expert, but I can provide you with sample code in C#. Since the ListBox control stores the data in a collection, you will need to access it in this way: Code: // Create an empty array to store the selected indices int[] tempArray = new int[] {}; // Invokes a method in the ListBox Control to retrieve the indices that are selected tempArray = emailParents.GetSelectedIndices(); // Iterate through each index that was selected foreach (int i in tempArray) mailMessage.Bcc.Add(New System.Net.Mail.MailAddress(emailParents.Items[i].Value.ToString()));
Okay, the code in Visual Basic would look something like this. You may have to tweak it a bit: Code: Dim tempArray(10) As Integer tempArray = emailParents.GetSelectedIndices() For Each i As Integer In tempArray mailMessage.Bcc.Add(New System.Net.Mail.MailAddress(Convert.ToString(emailParents.Items(i));
Small issue ok, the vb code works fine when all items are selected, but when only certain items are selected it sends email only to the last item selected. Is there a fix for that? If not, can I use the AccessDataSource directly and just email everyone, turns out we don't necessarily need the select functionality in the listbox. Either way would be fine. Essentially, I could eliminate the listbox from the form and then would like to loop through the BCC again, but your code only works for a list box. How can it be modified to use the values from the connectionstring StudentsEmail? Thank you
Ignore my last issue The code works just fine, some of the test addresses were routing the emails to spam. Everything works perfectly.