我正在开发一个Web应用程序,为用户提供简短的测验。系统将检查数据库中的Quiz表,该表由QuizID和IsSent列组成。
如果IsSent列(位数据类型)的值为0(为false),系统会将测验发送给所有用户。如果它有1,则表示测验已经发送。
我可以让应用程序发送电子邮件,但现在我希望系统在发送电子邮件后将IsSent的值更新为1或true,但我不知道该怎么做。
谁能告诉我怎么做?
我的代码:
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);
}
}
答案 0 :(得分:1)
您需要跟踪QuizID,我建议使用ArrayList,如下所示:
ArrayList quizIDs = new ArrayList();
在while
循环中,您遍历阅读器并将quizid
变量设置为reader["QuizID"].ToString()
,添加以下行以将quizid存储到列表中:
quizIDs.Add(quizid);
发送完所有电子邮件后,您需要执行查询,更新以前保存的所有QuizID的IsSent列。
这是您构建更新查询的方法:
string updateQuery = "UPDATE dbo.QUIZ SET IsSent = 1 WHERE QuizID in (" + String.Join(",", quizIDs) + ")";
答案 1 :(得分:1)
首先,您目前的代码只会发送有关上一次测验的电子邮件。
其次,作为一般经验法则,您应该只从数据库中检索绝对必要的数据。没有理由阅读已经发送的测验,因此您可以将该查询更改为:
string cmdText = "SELECT QuizID FROM dbo.QUIZ WHERE IsSent <> 1";
第三,您应该在发送测验的电子邮件后立即为每个测验更新IsSent。
最后,您应该发送一封包含所有收件人的电子邮件作为BCC用户,而不是多封电子邮件。
这是一个包含所有这些概念的重写:
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();
}
}