Reputation: 89
I have this method that will send Email to people according to my SQL SELECT, it works like this:
using (SqlCommand cmd = new SqlCommand("SELECT TITLE, RECIPIENT, SENDER FROM DAY2, CADUSER WHERE CADUSER.ID = ID_CADUSER", con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
sb.AppendLine(reader["TITLE"].ToString());
st.AppendLine(reader["RECIPIENT"].ToString());
se.AppendLine(reader["SENDER"].ToString());
}
}
SmtpClient smtpClient = new SmtpClient("mysmtp.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("______", "______");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
MailMessage mailMessage = new MailMessage(se.ToString(), st.ToString());
mailMessage.Subject = "New Documents";
mailMessage.Body = "The documents are: \n" + sb.ToString();
smtpClient.Send(mailMessage);
}
catch (ArgumentException)
{
}
The problem is that when I have more than 1 sender or recipient, It won't work because AppendLine command will join every result from my select in just one line like this: [email protected]@gmail.com making it impossible to send.
How can I separate one Email address by one to correct this issue? Thought about making a While routine but I don't know.
Upvotes: 0
Views: 59
Reputation: 261
In brief, try this:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
List<string> To = new List<string>();
To.Add("[email protected]");
To.Add("[email protected]");
foreach (var item in To)
{
mail.To.Add(to);
}
SmtpServer.Send(mail);
Check this out, there are many attributes to help you.
I hope this helps
Upvotes: 1