Hi im working on a software project with C# im stuck with this problem sending emails to specific emails works great now what i want is when i tick a checkbox emails from a table in database get in the add to Message etc...... this is my code:
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.Timeout = 10000;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
message.From = new MailAddress("[email protected]", "Mark Fenech");
if (chcbox_members.Checked)
{
message.To.Add(// here im adding the db Code to Select Email from tblMember
}
else
{
message.To.Add(txt_emailTo.Text);
}
message.Subject = txt_Subject.Text;
message.Body = label9.Text + " " + label5.Text + "\n"+ label10.Text + " " + label6.Text + "\n" + label11.Text + " " + label7.Text+ "\n" + label12.Text + " "+ label8.Text;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtp.Send(message);
This is an image of te design so you can understand more : http://i55.tinypic.com/wbtncm.jpg
Loading
Jonathas SucupiraPosted Apr 22, 2011, 3:32 PM
Please note that now I am calling the sub rotine that sends the email from the ReadOrderData.
Also you should put a try{} on your email function, like I did on the one from yesterday.
private void ReadOrderData()
{
string queryString =
"Your query here;";
using (SqlConnection connection =
new SqlConnection("Your Connection"))
{
SqlCommand command =
new SqlCommand(queryString, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
sendEmail(reader[0].ToString());
}
// Call Close when done reading.
reader.Close();
}
catch (Exception ex)
{
Response.Write("This error occurred= " + ex.Message);
}
finally
{
connection.Close();
command = null;
}
}
}
private void sendEmail(string message)
{
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.Timeout = 10000;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
message.From = new MailAddress("[email protected]", "Mark Fenech");
if (chcbox_members.Checked)
{
message.To.Add(// here im adding the db Code to Select Email from tblMember
}
else
{
message.To.Add(message);
}
message.Subject = txt_Subject.Text;
message.Body = label9.Text + " " + label5.Text + "\n"+ label10.Text + " " + label6.Text + "\n" + label11.Text + " " + label7.Text+ "\n" + label12.Text + " "+ label8.Text;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtp.Send(message);
}
}
Mark FenechPosted Apr 22, 2011, 7:05 PM
Jonathas SucupiraPosted Apr 22, 2011, 4:32 PM
http://www.eggheadcafe.com/tutorials/aspnet/04f92b99-2f84-4ca6-9a80-570d8f88d6b5/sending-mails-in-sql-server.aspx
Jonathas SucupiraPosted Apr 22, 2011, 3:29 PM
Mark FenechPosted Apr 22, 2011, 3:21 PM
Mark FenechPosted Apr 22, 2011, 3:12 PM
Jonathas SucupiraPosted Apr 22, 2011, 2:17 PM