我有一个包含Email_Adress列的表tb_register。其他人是Personna,Org_Name,First_Name,Last_Name,Email_Adress,Country。
我希望我的系统向数据库中的注册用户电子邮件地址发送电子邮件,具体取决于下拉菜单的管理员选择(首先下拉:全部,选择国家/地区)。
下拉菜单中包含All(电子邮件应发送到数据库中的所有电子邮件地址),或者如果他选择一个国家/地区,则会显示所有国家/地区的隐藏下拉菜单,并且会向注册到此国家/地区的用户发送电子邮件
我的第一个下拉菜单是ID是类别
我的ASP代码;
<%if (pgError.Text.Length >= 1){ %>
<div style="width:100%; text-align:left; padding:5px;">
<b><asp:Label ID="pgError" CssClass="pageErrors" runat="server"></asp:Label></b>
</div>
<% } %>
<div class="sendTo">
<table class="SendMail">
<tr>
<td class="SendTD">Send To:</td>
<td class="cateGory">
<asp:DropDownList ID="category" runat="server" OnSelectedIndexChanged="category_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Select Category</asp:ListItem>
<asp:ListItem>....................................</asp:ListItem>
</asp:DropDownList>
</td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Subcategory" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
<td colspan="2" class="AllDrops">
<div class="country">
<asp:DropDownList ID="Countryname" runat="server" Width="200px" Visible="False" >
<asp:ListItem>Select a Country</asp:ListItem>
<asp:ListItem>-------------------------</asp:ListItem>
</asp:DropDownList></div>
<div class="Regions">
<asp:DropDownList ID="Regions" runat="server" Width="200px" Visible="False" >
<asp:ListItem>Select a Region</asp:ListItem>
<asp:ListItem>-------------------------</asp:ListItem>
</asp:DropDownList></div>
<div class="personna">
<asp:DropDownList ID="Persona" runat="server" Width="150px" Visible="False">
<asp:ListItem>Choose Personna</asp:ListItem>
<asp:ListItem>.....................................</asp:ListItem>
</asp:DropDownList> </div> </td>
</tr>
<tr>
<td>Subject:</td>
<td class="Subject">
<asp:TextBox ID="subject" runat="server" Width="300px"></asp:TextBox><asp:Label ID="subjecterror" CssClass="pageErrors" runat="server"></asp:Label>
</td>
<td></td>
</tr>
<tr>
<td class="BodyTitle">Body:</td>
<td class="textArea">
<asp:TextBox ID="BoDyEmail" runat="server" Height="200px" TextMode="MultiLine" Width="520px"></asp:TextBox>
</td>
<td></td>
</tr>
<tr>
<td>Attachment:</td>
<td class="FileUpload">
<asp:FileUpload ID="FileAttachment" runat="server" />
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Send" runat="server" Text="Send Mail" OnClick="Send_Click" />
</td>
<td></td>
</tr>
</table>
</div>
我的C#代码
public partial class BulkSend : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//connection for the datareader
string csoWConn = ConfigurationManager.ConnectionStrings["RegisterCon"].ToString();
SqlConnection csoW_connection = new SqlConnection(csoWConn);
string database = csoW_connection.DataSource.ToString();
theSessionUserID.Text = Convert.ToString(Session["thisUserName"]);
//if the user is still logged in, load the user's known profile data
if (Session["thisUserID"] != null)
{
if (!IsPostBack)
{
try
{
csoW_connection.Open();
String categoryDDL = @"SELECT Category_ID, Category_Name FROM Email_Category ORDER BY Category_ID";
SqlCommand categoryTypeCmd = new SqlCommand(categoryDDL, csoW_connection);
SqlDataAdapter categorysAdapter = new SqlDataAdapter(categoryTypeCmd);
DataSet categoryType = new DataSet();
categorysAdapter.Fill(categoryType);
csoW_connection.Close();
//List of Category
category.DataSource = categoryType.Tables[0];
category.DataValueField = "Category_ID";
category.DataTextField = "Category_Name";
category.DataBind();
csoW_connection.Open();
String myCountriesDDL = @"SELECT country_ID, country_Name FROM tb_countries ORDER BY country_Name";
SqlCommand ctryCmd = new SqlCommand(myCountriesDDL, csoW_connection);
SqlDataAdapter countriesAdapter = new SqlDataAdapter(ctryCmd);
DataSet mycountries = new DataSet();
countriesAdapter.Fill(mycountries);
csoW_connection.Close();
//List of actor countries
Countryname.DataSource = mycountries.Tables[0];
Countryname.DataValueField = "country_ID";
Countryname.DataTextField = "country_Name";
Countryname.DataBind();
//UenpRegions type list
csoW_connection.Open();
String RegionDDL = @"SELECT * FROM tb_WorldRegion ORDER BY Region_ID";
SqlCommand RegionCmd = new SqlCommand(RegionDDL, csoW_connection);
SqlDataAdapter RegionsAdapter = new SqlDataAdapter(RegionCmd);
DataSet Regions = new DataSet();
RegionsAdapter.Fill(Regions);
csoW_connection.Close();
//List of Personna
Regions.DataSource = Regions.Tables[0];
Regions.DataValueField = "Region_ID";
Regions.DataTextField = "Region_Name";
Regions.DataBind();
csoW_connection.Open();
String PersonnaTypesDDL = @"SELECT Personna_ID, Personna_Type FROM tb_personna ORDER BY Personna_Type";
SqlCommand personnaTypeCmd = new SqlCommand(PersonnaTypesDDL, csoW_connection);
SqlDataAdapter organisationsAdapter = new SqlDataAdapter(personnaTypeCmd);
DataSet personnaType = new DataSet();
organisationsAdapter.Fill(personnaType);
csoW_connection.Close();
//List of Personna
Persona.DataSource = personnaType.Tables[0];
Persona.DataValueField = "Personna_ID";
Persona.DataTextField = "Personna_Type";
Persona.DataBind();
}
catch
{
Response.Write("Error");
}
}
}
else
{
Response.Redirect("Login.aspx");
}
}
protected void category_SelectedIndexChanged(object sender, EventArgs e)
{
string Check = category.SelectedItem.Text;
if (Check == "Country")
{
Countryname.Visible = true;
Subcategory.Visible = true;
Subcategory.Text = "Choose a Country";
Regions.Visible = false;
Persona.Visible = false;
}
if (Check == "Region") {
Countryname.Visible = false;
Regions.Visible = true;
Subcategory.Visible = true;
Subcategory.Text = "Choose a Region";
Persona.Visible = false;
}
if (Check == "Personna")
{
Persona.Visible = true;
Subcategory.Visible = true;
Subcategory.Text = "Choose a Personna";
Countryname.Visible = false;
Regions.Visible = false;
}
}
protected void Send_Click(object sender, EventArgs e)
{
//First name
if (subject.Text.Trim().Length < 1)
{
subjecterror.Text = " Please enter the <b>SUBJECT OF THE EMAIL</b>";
pgError.Text = subjecterror.Text;
}
else
{
subject.Text = subject.Text.Replace("'", "'");
}
try
{
}
catch { }
}
}
}
我试过这个但是错误......这是一个妥协,因为它不是我想做的但仍然无法正常工作
try{
string Bulksubject = subject.Text;
string body=BoDyEmail.Text;
foreach (GridViewRow item in LoadEmails.Rows)
{
string email = item.Cells[4].Text.Trim();
MailMessage BulkEmail = new MailMessage();
BulkEmail.From = new MailAddress("civil.society@me.org");
BulkEmail.To.Add(new MailAddress(email));
//BulkEmail.To.Add(email);
MailAddress copy = new MailAddress("izotieno@me.com");
BulkEmail.Bcc.Add(copy);
BulkEmail.Subject = Bulksubject;
BulkEmail.IsBodyHtml = true;
string htmlBody;
htmlBody = "<div style='margin-left:25%;margin-right:25%;margin-top:5%;border-width:1px; border-color:#CCCCCC; border-style:solid;' align='center'>"
+ body
+ "</div>";
BulkEmail.Body = htmlBody;
if (FileAttachment.HasFile)
{
BulkEmail.Attachments.Add(new Attachment(FileAttachment.PostedFile.InputStream, FileAttachment.FileName));
}
SmtpClient smtpServer = new SmtpClient();
smtpServer.Send(BulkEmail);
pgError.Text = "Mail sent successfully";
}
}
catch (Exception ex)
{
Response.Write("Exception Message: " + ex.Message);
pgError.Text = "We are expereinecing technical difficulties. Sorry";
}
/////////////////////////////////////////////// ////////////// GridView代码 ////////////////////////////////////////////////// ///////////////////////////
protected void Page_Load(object sender, EventArgs e)
{
//connection for the datareader
string csoWConn = ConfigurationManager.ConnectionStrings["RegisterCon"].ToString();
SqlConnection csoW_connection = new SqlConnection(csoWConn);
string database = csoW_connection.DataSource.ToString();
theSessionUserID.Text = Convert.ToString(Session["thisUserName"]);
//if the user is still logged in, load the user's known profile data
if (Session["thisUserID"] != null)
{
if (!IsPostBack)
{
try
{
fillgrid();
}
catch
{
Response.Write("Error Couldn't load data");
}
finally {
csoW_connection.Close();
}
}
public void fillgrid(){
//connection for the datareader
string csoWConn = ConfigurationManager.ConnectionStrings["RegisterCon"].ToString();
SqlConnection csoW_connection = new SqlConnection(csoWConn);
string database = csoW_connection.DataSource.ToString();
csoW_connection.Open();
DataTable dt;
String SQL = @"SELECT Registration_ID, Personna, Org_Name, First_Name, Last_Name, Email_Adress, country_Name FROM vw_ViewAll ORDER BY Registration_ID";
{
using (SqlCommand ctryCmd = new SqlCommand(SQL, csoW_connection))
{
using (SqlDataAdapter da = new SqlDataAdapter(ctryCmd))
{
dt = new DataTable("tbl");
da.Fill(dt);
}
}
}
LoadEmails.DataSource = dt;
LoadEmails.DataBind();
Cache["Data"] = dt;
}
////////////////////////////////// ASP GRIDVIEW /////////// ////////////////////
<div class="GridView">
<asp:GridView ID="LoadEmails" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" HorizontalAlign="Center" AllowPaging="True" PageSize="10" AutoGenerateColumns="False" OnRowDeleting="LoadEmails_RowDeleting" OnPageIndexChanging="LoadEmails_PageIndexChanging" OnRowEditing="LoadEmails_RowEditing" OnRowCancelingEdit="LoadEmails_RowCancelingEdit" OnRowUpdating="LoadEmails_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="Personna">
<EditItemTemplate>
<asp:TextBox ID="editPersonna" runat="server" Text='<%# Eval("Personna") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Personna") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Organization Name">
<EditItemTemplate>
<asp:TextBox ID="EditOrgName" runat="server" Text='<%# Eval("Org_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Org_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<EditItemTemplate>
<asp:TextBox ID="editFirstName" runat="server" Text='<%# Eval("First_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("First_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<EditItemTemplate>
<asp:TextBox ID="EditLastName" runat="server" Text='<%# Eval("Last_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Last_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<EditItemTemplate>
<asp:TextBox ID="EditEmail" runat="server" Text='<%# Eval("Email_Adress") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Email_Adress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<EditItemTemplate>
<asp:TextBox ID="editCountry" runat="server" Text='<%# Eval("country_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("country_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edition" ShowEditButton="True" />
<asp:CommandField HeaderText="Deletion" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="ID" Visible="False">
<EditItemTemplate>
<asp:Label ID="EditID" runat="server" Text='<%# Eval("Registration_ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("Registration_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerSettings
Position="Bottom"
Mode="NumericFirstLast"
PageButtonCount="20"
FirstPageText="First"
LastPageText="Last"
NextPageText="Next"
PreviousPageText="Prev"
/>
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</div>
我的网格从上到下从左到右依次具有以下字段 1.Personna 2.orgnaization名称 3.第一个名字 4.姓氏 5.Email 6.Country 7.Edition 8.Deletion 9.ID-隐藏
所以单元格[]。如果没有弄错,文字应为4,但我得到的错误是
异常消息:参数&#39;地址&#39;不能是一个空字符串。参数名称:地址
请帮忙
答案 0 :(得分:0)
试试这个
foreach(LoadEmails.Rows中的GridViewRow项) {
string email = item.Cells[4].Text.Trim();
if (!string.IsNullOrEmpty(email))
{
//CODE
}
我认为电子邮件是空白的
尝试调试以查看此内容,因为此处代码可以正常工作
答案 1 :(得分:0)
您没有直接将电子邮件地址添加到网格视图 - 您将值绑定到标签控件(如下所示)
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Email_Adress") %>'></asp:Label>
</ItemTemplate>
为了获得您首先需要获得对Label控件的引用(通过ID)的值,您可以获得Label Text属性,该属性将为您提供您的电子邮件地址,然后您可以继续使用其余的电子邮件发送代码 - 像这样
foreach (GridViewRow item in LoadEmails.Rows)
{
Label lblEmail = (Label)item.FindControl("Label5");
if(lblEmail != null)
{
string email = lblEmail.Text;
//do email sending
}
}