I am developing a web application that provides the users with short quizzes. The system will check the Quiz table in the database which consists of a QuizID and IsSent column.
If the IsSent column (which is a bit data type) has a value of 0 (which is false), the system will send the quiz to all users. If it has a 1, this means the quiz has already been sent.
I am able to let the application sends emails, but now I want the system to update the value of IsSent to 1 or true after sending the email but I don’t know how to do it.
Can anyone tell me how to do it?
My Code:
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);
}
}
First, your code as it currently stands, will only send an email about the last quiz.
Second, as a general rule of thumb, you should only retrieve the data that is absolutely necessary from the database. There is no reason to read in quizzes that have already been sent, so you can change that query to:
Third, you should update IsSent for each quiz immediately after the emails for the quiz have been sent.
Finally, you should send a single email with all recipients as BCC users rather than multiple emails.
Here is a rewrite containing all of these concepts: