0

我使用 ASP.NET 向导控件将新用户添加到我开发的基于 Web 的应用程序中。一切正常。现在,我从系统管理员那里得到了一个新要求,它正在向新用户发送电子邮件通知,告诉他他已被添加到系统中。我将 Mail 函数添加到我的代码中并且它可以工作。现在,单击向导上的完成按钮后,用户将被添加到数据库中,系统将向用户发送电子邮件通知,然后向导将显示成功消息。但是,有时系统需要很长时间才能将该电子邮件发送给用户并且向导向我显示成功消息。

那么,在系统向用户发送电子邮件时,如何显示(请稍候)屏幕/弹出窗口?

带有邮件功能的代码隐藏:

 protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {

        string username = TextBox1.Text;

        if (!String.IsNullOrEmpty(radio1.SelectedValue) && !String.IsNullOrEmpty(username))
        {
            string connString = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True";

            string insertUserCommand = "INSERT INTO employee (Name, Username, JobTitle, BadgeNo, EmpOrgType, DivisionCode) values (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)";
            string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                // Open DB connection.
                using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                {
                    if ((int)cmd.ExecuteScalar() == 0){

                        SqlCommand cmd2 = new SqlCommand(insertUserCommand, conn);
                        cmd2.Parameters.AddWithValue("@Name", user.Name);
                        cmd2.Parameters.AddWithValue("@Username", username);
                        cmd2.Parameters.AddWithValue("@JobTitle", jobTitle);
                        cmd2.Parameters.AddWithValue("@BadgeNo", EmpNo.));
                        cmd2.Parameters.AddWithValue("@EmpOrgType", orgType);
                        cmd2.Parameters.AddWithValue("@DivisionCode", orgCode);
                        cmd2.ExecuteNonQuery();
                    }

                }
            }

            //For updating the role of the user
            string deleteCommand = "DELETE FROM UserRole where Username=@Username";
            string insertCommand = "INSERT INTO UserRole (RoleID,Username) values(@RoleID,@Username)";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                using (SqlCommand cmd = new SqlCommand(deleteCommand, conn))
                {
                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.ExecuteNonQuery();
                    //Now the insert
                    cmd.CommandText = insertCommand;
                    cmd.Parameters.Clear(); 
                    cmd.Parameters.AddWithValue("@RoleID", radio1.SelectedValue);
                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.ExecuteNonQuery();
                    //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                    //cmd.ExecuteScalar();
                    //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
                }
            }

            Wizard1.Visible = false;
            wizard.InnerHtml = @"<p><b>The task has been done successfully.</b> <br /> <a href='UserManagement.aspx'>Edit Another User</a></p>";
        }

        SendEmailToUser(username);

    }
/*****************************************************/


    protected void SendNotificationByEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
    {
        SmtpClient sc = new SmtpClient("MailAddress");
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("test@mailAddress.com", "Test Sys");


            msg.Bcc.Add(toAddresses);
            msg.Subject = MailSubject;
            msg.Body = MessageBody;
            msg.IsBodyHtml = isBodyHtml;
            sc.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

    protected void Send(string username)
    {
        string connString = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True";

        string networkID = username.ToString();

        using (SqlConnection conn = new SqlConnection(connString))
        {
            var sbEmailAddresses = new System.Text.StringBuilder(2000);

            //initiate the varibles that will be retreived from the database
            string name = null;

            // Open DB connection.
            conn.Open();

            string cmdText2 = @"SELECT     Name
                                FROM       dbo.employee
                                WHERE     (Username = @networkID)";
            using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
            {
                cmd.Parameters.AddWithValue("@networkID", networkID);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    if (reader.Read())
                    {
                        name = reader["Name"].ToString();
                        sbEmailAddresses.Append(username).Append("@mailAddress.com");
                    }
                }

                //var sEMailAddresses = sbEmailAddresses.ToString();
                string body = @"...........................";
                SendNotificationByEmail(sbEmailAddresses.ToString(), "", "Welcome...", body, true);
                sbEmailAddresses.Clear();
                reader.Close();


            }

            conn.Close();
        }
    }
4

1 回答 1

2

如果您的应用程序使用UpdatePanel,您可以使用该UpdateProgress控件在请求处理时显示您想要的任何内容。

于 2012-07-21T07:31:43.347 回答