如何从GridView向所选事件中的已注册用途发送电子邮件提醒?

时间:2012-12-22 09:00:50

标签: c# asp.net sql-server-2008-r2 sendmail

我是一名新的ASP.NET开发人员,我正在开发一个简单的注册系统。我现在正在尝试让系统管理员能够在事件开始日期之前向注册用户发送提醒。所有可用事件都将列在GridView中,其中包含一个按钮,用于向该事件中的注册用户发送提醒。 为了您的信息,我有以下数据库设计:

Employees Table: NetID, Name
Events Table: ID, Title
BookingDetails Table: BookingID, EventID, NetID

* *请注意,NetID是员工的用户名

ASP.NET代码:

<asp:GridView ID="ListOfAvailableEvents_GrivView" runat="server" 
            AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
                    GridLines="None" AllowPaging="True">
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
                    <Columns>
                        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                        <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
                        <asp:BoundField DataField="StartDateTime" HeaderText="Start Date & Time" SortExpression="StartDateTime" />
                        <asp:BoundField DataField="EndDateTime" HeaderText="End Date & Time" SortExpression="EndDateTime" />
                        <asp:BoundField DataField="NumberOfSeats" HeaderText="Number of Seats" SortExpression="NumberOfSeats" />
                        <asp:BoundField DataField="Number of Bookings" HeaderText="Number of Bookings" ReadOnly="True"
                            SortExpression="Number of Bookings" />
                        <asp:TemplateField HeaderText="">
                            <ItemTemplate>
                                <asp:Button ID="sendButton" runat="server" CssClass="button" Text="Send Reminder &rarr;"
                                    OnClick="btnSend_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle Font-Bold="True" CssClass="complete" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                </asp:GridView>

                <asp:HiddenField ID="HiddenField1" runat="server" />

                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PM_RegistrationSysDBConnectionString %>"

            SelectCommand="SELECT Events.Title, Events.Description, Events.Location, Events.StartDateTime, Events.EndDateTime, Events.NumberOfSeats, COUNT(BookingDetails.BookingID) AS [Number of Bookings] FROM Events INNER JOIN BookingDetails ON Events.ID = BookingDetails.EventID WHERE (Events.IsActive = 1) GROUP BY Events.Title, Events.Description, Events.Location, Events.StartDateTime, Events.EndDateTime, Events.NumberOfSeats ORDER BY Events.StartDateTime DESC"></asp:SqlDataSource>

C#代码:

protected void btnSend_Click(object sender, EventArgs e)
    {
        GridViewRow gvrow = (GridViewRow)(((Button)sender)).NamingContainer;
        HiddenField1.Value = ListOfAvailableEvents_GrivView.DataKeys[gvrow.RowIndex].Value.ToString();

        string title = gvrow.Cells[0].Text;
        string description = gvrow.Cells[1].Text;
        string location = gvrow.Cells[2].Text;
        string startDateTime = gvrow.Cells[3].Text;
        string endDateTime = gvrow.Cells[4].Text;

        string connString = ".............................;";

        using (SqlConnection conn = new SqlConnection(connString))
        {
            var sbEmailAddresses = new System.Text.StringBuilder(2000);
            int eventID = Convert.ToInt32(HiddenField1.Value);

            //Open DB connection
            conn.Open();

            string cmdText = @"SELECT     dbo.Events.Title, dbo.Employees.NetID
                                FROM         dbo.BookingDetails INNER JOIN
                                        dbo.Events ON dbo.BookingDetails.EventID = @EventID INNER JOIN
                                        dbo.Employees ON dbo.BookingDetails.NetID = dbo.Employees.NetID";
            using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 
            {
                cmd.Parameters.AddWithValue("@EventID", eventID);
                cmd.ExecuteNonQuery();

                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        var sName = reader.GetString(0);
                        if (!string.IsNullOrEmpty(sName))
                        {
                            if (sbEmailAddresses.Length != 0)
                            {
                                sbEmailAddresses.Append(", ");
                            }
                            sbEmailAddresses.Append(sName).Append("@appServer.com");
                        }
                    }
                }
                reader.Close();

                var sEMailAddresses = sbEmailAddresses.ToString();
                string body = "..........................";


                int sendCount = 0;
                List<string> addressList = new List<string>(sEMailAddresses.Split(','));
                StringBuilder addressesToSend = new StringBuilder();

                for (int userIndex = 0; userIndex < addressList.Count; userIndex++)
                {
                    sendCount++;
                    if (addressesToSend.Length > 0)
                        addressesToSend.Append(",");

                    addressesToSend.Append(addressList[userIndex]);
                    if (sendCount == 10 || userIndex == addressList.Count - 1)
                    {
                        SendEmail(addressesToSend.ToString(), "", "Registration REMINDER Notification", body, true);
                        addressesToSend.Clear();
                        sendCount = 0;
                    }
                }

                cmd.ExecuteNonQuery();
            }
            //reset the value of hiddenfield
            HiddenField1.Value = "-1";
        }
    }

    protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
    {
        SmtpClient sc = new SmtpClient("MAIL ADDRESS");
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("app@appServer.com", "Registration System");

            // In case the mail system doesn't like no to recipients. This could be removed
            //msg.To.Add("app@appServer.com");

            msg.Bcc.Add(toAddresses);
            msg.Subject = MailSubject;
            msg.Body = MessageBody;
            msg.IsBodyHtml = isBodyHtml;
            sc.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
            // something bad happened
            //Response.Write("Something bad happened!");

        }

    }

但我无法发送任何电子邮件。你能告诉我为什么吗?

更新:

我写了代码,当我运行它时,我没有收到任何错误。我尝试调试它,当我这样做时,我发现对SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)方法的调用不起作用,我不知道为什么。

那么请您告诉我如何向所选活动的注册用户发送电子邮件?

1 个答案:

答案 0 :(得分:1)

for (int userIndex = 0; userIndex < addressList.Count-1; userIndex++)
                {

检查此循环一次