2011-03-22 56 views
0

我在ASP.NET C#中创建一个表单,以便可以填写并通过电子邮件发送给多个收件人。表单的一部分是一个复选框部分,其中包含多个选项。我只能选择第一个选项通过电子邮件发回给收件人,所以如果用户选择两个或多个复选框,它只会通过电子邮件发送第一个选项。以下是我的代码单ASP.NET复选框控件

SmtpClient smtpClient = new SmtpClient(); 
    MailMessage message = new MailMessage(); 
    MailAddress From = new MailAddress(mailTextBox.Text); 
    message.To.Add(new MailAddress("[email protected]")); 
    message.Subject = (companyTextBox.Text); 
    message.IsBodyHtml = true; 
    message.Body = "<html><head></head><body>" + 
    "<p></p>" + 
    "<p>Business Type: " + typeDropDownList.Text + "</p>" + 
    "<p>Company: " + companyTextBox.Text + "</p>" + 
    "<p>Name: " + nameTextBox.Text + "</p>" + 
    "<p>Address: " + addressTextBox.Text + "</p>" + 
    "<p>City: " + cityTextBox.Text + "</p>" + 
    "<p>State: " + stateDropDownList.Text + "</p>" + 
    "<p>Zip Code: " + zipcodeTextBox.Text + "</p>" + 
    "<p>Phone Number: " + phoneTextBox.Text + "</p>" + 
    "<p>Email: " + mailTextBox.Text + "</p>" + 
    "<p>Number Of Locations: " + locationsDropDownList.Text + "</p>" + 

    **// This is my problem area //** 
    "<p>Interested In: " + interestedCheckBoxList.Text + "</p>" + 
    "<p>Interested In: " + interestedCheckBoxList.Text + "</p>" + 
    "<p>Interested In: " + interestedCheckBoxList.Text + "</p>" + 
    **// This is my problem area //** 

    "<p>Message: " + messageTextBox.Text + "</p>" + 
    "</body></html>"; 
    smtpClient.Send(message); 
    Response.Redirect("http://www.domain.com"); 

在此先感谢您。

吉姆

+0

你正在采取相同的3行代码并复制它。 – Cyberdrew 2011-03-22 18:38:56

+2

我也推荐使用StringBuilder。 – Cyberdrew 2011-03-22 18:39:43

+0

此外,CheckBox控件和asp.net中的CheckBoxList控件有所不同。小心不要让他们困惑。 – 2011-03-22 18:41:07

回答

1

你需要通过你的CheckBoxList迭代,并找到所有的检查项目,并得到了Text财产的每个项目和附加到你的电子邮件文本。

string yourSelectedList = ""; 
foreach (ListItem i in chklst.Items) 
{ 
    if (i.Selected) 
     yourSelectedList += (i.Text + ", "); 
} 

那么,在年底删除多余的逗号:)

"<p>Interested In: " + yourSelectedList + "</p>" + 

尝试concatentating许多串在一起时使用StringBuilder,因为它会带来很大的区别。

2

您需要遍历CheckBoxList中的Items并逐个添加它们。

例子:

foreach(ListItem li in interestedCheckBoxList.Items) 
{ 
    //add your stuff 
    if(li.Selected) 
    { 
     //should be using string builder here but.... 
     message.Body += "<p>Interested In: " + li.Text + "</p>"; 
    } 
} 
0

尝试用以下替换代码在您的“问题区域”:

string InterestedIn = ""; 
foreach (ListItem li in interestedCheckBoxList.Items) 
{ 
    if (li.Selected) 
     InterestedIn += "<p>Interested In: " + li.Text + "</p>"; 
} 

当然,你不能连接这是你原来的字符串连接的一部分,因此请构建一个“InterestedIn”字符串并将电子邮件正文连接起来。