0

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>
3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by jv_web
1

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>
Votes + Comments
Good example :)
0

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

0

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...

0

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 :)

0

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

0

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());
		}
    }
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.