Hello,

Im trying to generate an email with Multiple Listbox selections... Unfortunately the listbox allows me to select multiple choice but when I submit the form only one "selection" is shown... Im assuming a for each or something will be needed here... all tests using that method have failed. Could someone point me in the right direct or show me?

<script runat="server"> 

void btnSubmit_Click(Object sender, EventArgs e) {

MailMessage objEmail = new MailMessage();
objEmail.To = txtTo.Text;
objEmail.From = txtFrom.Text;
objEmail.Subject = "Sample Form";
objEmail.Body = "<h2>Sample Form Title</h2>" + "<br />" + listType.SelectedItem.Text + "<br />"; 
objEmail.Priority = MailPriority.High;
objEmail.BodyFormat = MailFormat.Html;


try{
SmtpMail.Send(objEmail);
Response.Write("Email Sent - Thank You!");
}
catch (Exception exc){
Response.Write("Send failure: " + exc.ToString());
}
}
</script>

Yes you are right, you need to iterate through all the selected items from listbox using foreach loop. I believe you want to send an email to multiple person/address selected in listbox. If so then your code may looks like below :

foreach (ListItem item in ListBox1.Items)
{
    if (item.Selected)
    {
       objEmail.To.Add(new MailAddress(item.Value));
    }
}

remaining code will be as it is..

that's all.. Try it and let us know..

Hello,

Im trying to generate an email with Multiple Listbox selections... Unfortunately the listbox allows me to select multiple choice but when I submit the form only one "selection" is shown... Im assuming a for each or something will be needed here... all tests using that method have failed. Could someone point me in the right direct or show me?

<script runat="server"> 

void btnSubmit_Click(Object sender, EventArgs e) {

MailMessage objEmail = new MailMessage();
objEmail.To = txtTo.Text;
objEmail.From = txtFrom.Text;
objEmail.Subject = "Sample Form";
objEmail.Body = "<h2>Sample Form Title</h2>" + "<br />" + listType.SelectedItem.Text + "<br />"; 
objEmail.Priority = MailPriority.High;
objEmail.BodyFormat = MailFormat.Html;


try{
SmtpMail.Send(objEmail);
Response.Write("Email Sent - Thank You!");
}
catch (Exception exc){
Response.Write("Send failure: " + exc.ToString());
}
}
</script>
commented: Good example :) +1

Unfortunately I wish to add the "selected items" ( listType.SelectedItem.Text ) to the body of the email...

did your problem solve or not ? if solved then please close the thread

Unfortunately I wish to add the "selected items" ( listType.SelectedItem.Text ) to the body of the email...

Unfortunately I wish to add the "selected items" ( listType.SelectedItem.Text ) to the body of the email...

Just because Rohand's specific example utilized the objEmail.To.Add() example doesn't mean that would be the only thing that would work in this scenario.

Simply replace the unneeded segment with the logic required to insert the specified text to the body of the email as needed and the foreach loop should do what you need.

As indicated, however, if the issue is actually solved, please mark it as solved (link next to the reply text area) so that others (like myself) don't think that more work still needs to be done on this thread :)

Thank you for the prompt response... I will definitely try this out and mark solved (if it works, which it seems it will)

Thanks All

unfortunately... I dont know how to add to the body of the email properly...

This is what I have so far. Can someone please advise.

Thank you

void btnSubmit_Click(Object sender, EventArgs e) {
	
		DateTime now = DateTime.Now;

		MailMessage objEmail = new MailMessage();
		objEmail.To = txtTo.Text;
		objEmail.From = txtFrom.Text;
		objEmail.Cc = txtCc.Text;
		objEmail.Bcc = txtBcc.Text;
		objEmail.Subject = "Sample Form";
		objEmail.Body = "<br /><b>Vehicles Sold:</b>&nbsp;" + listType.SelectedItem.Text + "<br />"; 
		objEmail.Priority = MailPriority.High;
		objEmail.BodyFormat = MailFormat.Html;
		
		foreach (ListItem item in listType.Items)
		{
			if (item.Selected)
			{
			   objEmail.Body.Add(item.Value);
			}
		}

		try{
			SmtpMail.Send(objEmail);
			Response.Write("Email Sent - Thank You!");
		}
		catch (Exception exc){
			Response.Write("Send failure: " + exc.ToString());
		}
    }

I was actually able to solve this by adding a string value and re-working the foreach loop... Thank you all for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.