i am trying to send all the email listed in my GridView but for somereason, the email does not get sent out. I am suspecting my Send function (smtpClient.Send(mailMessage); is not working or i am missing something. Pls help as i have spent so many hours on figuring out this. thanks
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach(GridViewRow gr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("chkItem");
if(((CheckBox)sender).Checked)
cb.Checked = true;
else
cb.Checked = false;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(GridViewRow gr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("chkItem");
if(cb.Checked)
{
sb.Append(GridView1.DataKeys[gr.RowIndex]["Email"].ToString());
sb.Append(",");
}
}
//Create instance of main mail message class.
System.Net.Mail.MailMessage mailMessage=new System.Net.Mail.MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress(
System.Configuration.ConfigurationManager
.AppSettings["fromEmailAddress"]);
mailMessage.Priority = System.Net.Mail.MailPriority.High;
//Text/HTML
mailMessage.IsBodyHtml = false;
mailMessage.Body = "Hello, here is new email";
mailMessage.Subject = "RCA APPROVAL IS REQUIRED";
System.Net.Mail.SmtpClient smtpClient=new System.Net.Mail.SmtpClient();
try
{
smtpClient.Send(mailMessage);
Response.Write("<B>Email Has been sent successfully.</B>");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
You need to set the
Toaddress for theMailMessageYou might want to do it like:
Moreover if your
SMTPserver needs authentication you need to provide theCredentialsalso. Or you can put all the settings into<system.net><mailSettings>tag in web.config as shown here by none other than the mightly Scott Gu