My question is this: I am studying C#
and I am working on a project (study only) of student registration. I made a form to send e-mail if the student misses the school, except that I did not implement the faults column and I am doing the test with the school year. I make select email from Cadastro where anoletivo = 2010
. I registered about 5 students with different emails and with the 2010 school year. Only, I wanted to perform this select, store the emails in a list so I can pass this list to my method of sending e- mails. How could I do that?
Code:
CLASSE DAL
public List<ModeloAniversariante> CarregaModeloAniversariante()
{
List<ModeloAniversariante> lst = new List<ModeloAniversariante>();
try
{
ModeloAniversariante modelo = new ModeloAniversariante();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conexao.ObjetoConexao;
cmd.CommandText = "select email from Cadastro where anoletivo = 2010";
conexao.Conectar();
SqlDataReader registro = cmd.ExecuteReader();
while (registro.Read())
{
modelo.EmailResponsavel = Convert.ToString(registro["email"]);
lst.Add(modelo);
}
return lst;
}
catch (SqlException er)
{
throw new Exception(er.Message);
}
finally
{
conexao.Desconectar();
}
}
MODELO
public class ModeloAniversariante
{
private List<string> _emailresponsavel;
public List<string> EmailResponsavel
{
get { return this._emailresponsavel; }
set { this._emailresponsavel = value; }
}
NO "BOTÃO"
private void enviarEmailAosResponsáveisToolStripMenuItem_Click(object sender, EventArgs e)
{
DALConexao cx = new DALConexao(DadosDaConexao.StringDeConexao);
BLLAniversariante bll = new BLLAniversariante(cx);
ModeloAniversariante modelo = bll.CarregaModeloAniversariante(3);
string responsavel = Convert.ToString(modelo.EmailResponsavel);
Email = new MailMessage();
Email.To.Add(new MailAddress(responsavel));
Email.From = (new MailAddress("[email protected]"));
Email.Subject = "Teste";
Email.IsBodyHtml = true;
Email.Body = "Isso é um teste";
SmtpClient cliente = new SmtpClient("smtp.live.com", 587);
using (cliente)
{
cliente.Credentials = new System.Net.NetworkCredential("[email protected]", "minhasenha");
cliente.EnableSsl = true;
cliente.Send(Email);
}
MessageBox.Show("E-mail enviado com sucesso!", "Sucesso");
}