title: Send mail Based on drop down list c#
body: <p>I am trying to make an application where emails can be sent to users based on the choice (Service Type) from a drop down list. My code doesn't work as I deterred the Number type based on the service type as shown in the SQL query. My question is how can I send emails based on the service for each number I've got? Any help would be appreciated.</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              BindReportsTypeDropDown();
          }
      }
  
    public class EmailSend
      {
  
          public string Number { get; set; }
          public string Service  { get; set; }
  
      }
  
      protected void btnSend_Click(object sender, EventArgs e)
      {
          //try
          // {
          int Service = 0;
  
              // Connection String 
              string ConnectionString = @"Data Source= (localdb)\......";
              SqlDataReader reader;
              String SendMessage = "Select Number FROM services where   
               ServiceType = 1 or ServiceType =2";
              using (SqlConnection MyCon = new 
          SqlConnection(ConnectionString))
              {
                  MyCon.Open();
                  SqlCommand cmd = new SqlCommand(SendMessage, MyCon);
                  ArrayList EmailArray = new ArrayList();
                  reader = cmd.ExecuteReader();
  
                  var Email = new List&lt;EmailSend&gt;();
  
                  while (reader.Read())
                  {
  
                      Email.Add(new EmailSend
                      {
  
                          Number = Convert.ToString(reader["Numnber"]),
                          SErviceType = 
                 Convert.ToString(reader["ServiceType"]),
  
                      });
                  }
  
  
  
                  foreach (EmailSend email in Email)
                  {
                      // Email Config
  
                      const string username = "mail";
                      const string password = "pass";
                      SmtpClient smtp = new SmtpClient();
                      MailMessage mail = new MailMessage();
                      MailAddress fromadd = new MailAddress("email", "pass");
                      smtp.Host = "smtp.gmail.com";
                      smtp.Port = 587;
  
                      mail.From = fromadd;
                      mail.To.Add("mail");
                      mail.Subject = ("subject ");
  
                      mail.IsBodyHtml = true;
                      mail.Body = "&lt;p&gt;Hi &lt;/p&gt; " + HttpUtility.HtmlEncode(email.Number) + "&lt;br/&gt; &lt;p&gt;Please find the the details of all reports!&lt;/p&gt; &lt;/br&gt; &lt;p&gt; Date of Sending the reports: " + HttpUtility.HtmlEncode(email.ServiceType) + "&lt;/p&gt; &lt;br/&gt;"; 
                      smtp.EnableSsl = true;
                      smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                      smtp.Credentials = new System.Net.NetworkCredential(username, password);
                      smtp.Send(mail);
  
  
                  }
              }
          }
  </code></pre>
  
  <p>Drop Down List</p>
  
  <pre><code>  protected void BindReportsTypeDropDown()
      {
          ReportType.DataSource = GetData(" SELECT Number FROM Services where 
          ServiceType= 1 OR ServiceType= 2");
          ReportType.DataTextField = " ServiceType";
          ReportType.DataValueField = " ServiceType";
          ReportType.DataBind();
      }
      private DataTable GetData(string query)
      {
         string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
  
          SqlCommand cmd = new SqlCommand(query);
          using (SqlConnection con = new SqlConnection(conn))
          {
              using (SqlDataAdapter sda = new SqlDataAdapter())
              {
                  cmd.Connection = con;
  
                  sda.SelectCommand = cmd;
                  using (DataTable dt = new DataTable())
                  {
                      sda.Fill(dt);
                      return dt;
                  }
              }
          }
      }
  </code></pre>