Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于下拉列表c发送邮件#_C#_C# 4.0 - Fatal编程技术网

C# 基于下拉列表c发送邮件#

C# 基于下拉列表c发送邮件#,c#,c#-4.0,C#,C# 4.0,我正在尝试制作一个应用程序,可以根据下拉列表中的选择(服务类型)向用户发送电子邮件。我的代码不起作用,因为我根据SQL查询中显示的服务类型确定了数字类型。我的问题是,如何根据我拥有的每个号码的服务发送电子邮件?任何帮助都将不胜感激 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindReportsTypeDropDown();

我正在尝试制作一个应用程序,可以根据下拉列表中的选择(服务类型)向用户发送电子邮件。我的代码不起作用,因为我根据SQL查询中显示的服务类型确定了数字类型。我的问题是,如何根据我拥有的每个号码的服务发送电子邮件?任何帮助都将不胜感激

 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<EmailSend>();

                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 = "<p>Hi </p> " + HttpUtility.HtmlEncode(email.Number) + "<br/> <p>Please find the the details of all reports!</p> </br> <p> Date of Sending the reports: " + HttpUtility.HtmlEncode(email.ServiceType) + "</p> <br/>"; 
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Credentials = new System.Net.NetworkCredential(username, password);
                    smtp.Send(mail);


                }
            }
        }
  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;
                }
            }
        }
    }