C# { //只有一列,所以只需使用序号位置检索它 Add(reader.GetInt32(0)); } } reader.Close(); } string cmdText2=“从dbo.employee中选择用户名”; 使用(SqlCommand cmd=n

C# { //只有一列,所以只需使用序号位置检索它 Add(reader.GetInt32(0)); } } reader.Close(); } string cmdText2=“从dbo.employee中选择用户名”; 使用(SqlCommand cmd=n,c#,asp.net,C#,Asp.net,{ //只有一列,所以只需使用序号位置检索它 Add(reader.GetInt32(0)); } } reader.Close(); } string cmdText2=“从dbo.employee中选择用户名”; 使用(SqlCommand cmd=newsqlcommand(cmdText2,conn)) { SqlDataReader=cmd.ExecuteReader(); if(读卡器!=null) { while(reader.Read()) { var sName=reader.

{ //只有一列,所以只需使用序号位置检索它 Add(reader.GetInt32(0)); } } reader.Close(); } string cmdText2=“从dbo.employee中选择用户名”; 使用(SqlCommand cmd=newsqlcommand(cmdText2,conn)) { SqlDataReader=cmd.ExecuteReader(); if(读卡器!=null) { while(reader.Read()) { var sName=reader.GetString(0); 如果(!string.IsNullOrEmpty(sName) { 如果(sbEmailAddresses.Length!=0) { sbEmailAddresses。追加(“,”); } //只需使用用户名的序号位置,因为只有一列 sbEmailAddresses.Append(sName.Append)(@gmail.com)); } } } reader.Close(); } string cmdText3=“UPDATE dbo.quick SET IsSent=1,其中QuizId=@QuizId”; 使用(SqlCommand cmd=newsqlcommand(cmdText3,conn)) { //将参数添加到命令中 var opparameter=cmd.Parameters.Add(“@QuizID”,SqlDbType.Int); //获取电子邮件地址的本地副本 var sEMailAddresses=sbEmailAddresses.ToString(); foreach(在quizIds中输入quizid) { 字符串链接=”; string body=@“请尝试参加新的简短安全测试” +链接+ @“

此电子邮件是使用生成的。 请不要回复此电子邮件。 "; SendEmail(语义地址,“,”通知电子邮件主题“,正文,true); //更新当前测验的参数 oparmeter.Value=quizid; //然后执行命令 cmd.ExecuteNonQuery(); } } 康涅狄格州关闭(); } }
谢谢您的帮助。我发现一个错误:在邮件标题中发现一个无效字符:“@”,我不知道如何修复它。有什么帮助吗?对不起,代码中有一个错误,我已更新了答案。您需要将
if(sbEmailAddresses.Length==0)
更改为
if(sbEmailAddresses.Length!=0)
。我还进行了更新,以确保在将名称添加到地址列表之前从数据库中读取了名称。非常感谢您的帮助。我可以找到它。我在您的代码中做了一点更改。伙计,谢谢。您确实救了我的命,但现在我在您的代码中面临另一个问题。问题是:我想要系统如果有多个测验没有发送给用户,我将检查测验表。我希望系统只发送这些测验中的第一个,而不是所有测验或最后创建的测验。如何操作?
protected void Page_Load(object sender, EventArgs e)
{
    SendEmailTOAllUser();
}


protected void SendEmail(string toAddress, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml) 
{
    SmtpClient sc = new SmtpClient("SMTP (MAIL) ADDRESS");
    try
    {
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("pssp@gmail.com", "OUR SYSTEM");
        msg.To.Add(toAddress);
        msg.Subject = MailSubject;
        msg.Body = MessageBody;
        msg.IsBodyHtml = isBodyHtml;
        //Response.Write(msg);
        sc.Send(msg);
    }
    catch (Exception ex)
    {
        throw ex;
    }

}

protected void SendEmailTOAllUser()
{
    string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspTest;Integrated Security=True";
    string cmdText = "SELECT QuizID, IsSent FROM dbo.QUIZ";
    string cmdText2 = "SELECT Username FROM dbo.employee";

    Collection<string> emailAddresses = new Collection<string>();
    string link = "";
    string body = "";

    using (SqlConnection conn = new SqlConnection(connString))
    {
        conn.Open();
        // Open DB connection.
        using (SqlCommand cmd = new SqlCommand(cmdText, conn))
        {
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    if (!(bool)reader["IsSent"])
                    {
                        string quizid = reader["QuizID"].ToString();
                        link = "<a href='http://pmv/pssp/StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
                        body = @"<b> Please try to participate in the new short safety quiz </b>"
                                            + link +
                                            @"<br /> <br />
                        This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal </a>. 
                        Please do not reply to this email.
                        ";    
                    }
                }
            }
            reader.Close();
        }

        using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
        {
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    string emailTo = reader["Username"].ToString();
                    string receiverEmail = emailTo + "@gmail.com";
                    emailAddresses.Add(receiverEmail);
                }
            }
            reader.Close();
        }
        conn.Close();
    }

    foreach (string email in emailAddresses)
    {
        SendEmail(email, "", "Notification Email Subject", body, true);
    }
}
ArrayList quizIDs = new ArrayList();
quizIDs.Add(quizid);
string updateQuery = "UPDATE dbo.QUIZ SET IsSent = 1 WHERE QuizID in (" + String.Join(",", quizIDs) + ")";
string cmdText = "SELECT QuizID FROM dbo.QUIZ WHERE IsSent <> 1";
    protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
    {
        SmtpClient sc = new SmtpClient("SMTP (MAIL) ADDRESS");
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("pssp@gmail.com", "OUR SYSTEM");

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

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

    }

    protected void SendEmailTOAllUser()
    {
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspTest;Integrated Security=True";

        using (SqlConnection conn = new SqlConnection(connString))
        {
            var sbEmailAddresses = new System.Text.StringBuilder(1000);
            var quizIds = new List<int>();

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

            string cmdText = "SELECT QuizID FROM dbo.QUIZ WHERE IsSent <> 1";
            using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            {
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        // There is only 1 column, so just retrieve it using the ordinal position
                        quizIds.Add(reader.GetInt32(0));
                    }
                }
                reader.Close();
            }

            string cmdText2 = "SELECT Username FROM dbo.employee";
            using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
            {
                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(",");
                            }
                            // Just use the ordinal position for the user name since there is only 1 column
                            sbEmailAddresses.Append(sName).Append("@gmail.com");
                        }
                    }
                }
                reader.Close();
            }

            string cmdText3 = "UPDATE dbo.Quiz SET IsSent = 1 WHERE QuizId = @QuizID";
            using (SqlCommand cmd = new SqlCommand(cmdText3, conn))
            {
                // Add the parameter to the command
                var oParameter = cmd.Parameters.Add("@QuizID", SqlDbType.Int);
                // Get a local copy of the email addresses
                var sEMailAddresses = sbEmailAddresses.ToString();

                foreach (int quizid in quizIds)
                {
                    string link = "<a href='http://pmv/pssp/StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
                    string body = @"<b> Please try to participate in the new short safety quiz </b>"
                                        + link +
                                        @"<br /> <br />
                    This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal </a>. 
                    Please do not reply to this email.
                    ";

                    SendEmail(sEMailAddresses, "", "Notification Email Subject", body, true);

                    // Update the parameter for the current quiz
                    oParameter.Value = quizid;
                    // And execute the command
                    cmd.ExecuteNonQuery();
                }
            }
            conn.Close();
        }
    }