我正在尝试创建一个应用程序,根据下拉列表中的选项(服务类型)将电子邮件发送给用户。我的代码不起作用,因为我根据SQL查询中显示的服务类型阻止了Number类型。我的问题是如何根据我所获得的每个号码的服务发送电子邮件?任何帮助将不胜感激。
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;
}
}
}
}