我有一个查询,用于在ASP.net中发送电子邮件,但在现有代码中,我只能使用电子邮件中查询的两列:
名字(电子邮件)。
emails[Convert.ToString(reader["email"])] = Convert.ToString(reader["FirstName"]);
现在我想改进代码,以便我可以使用
这是我之前检索两列
的代码 SqlDataReader reader;
string sendMessage = "SELECT aspnet_Membership.Email, User_Profile.FirstName, User_Profile.Program FROM aspnet_Membership join User_Profile on User_Profile.UserId = aspnet_Membership.UserId";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(sendMessage, myConnection);
myCommand.Parameters.AddWithValue("@UserId", currentUserId);
myCommand.Parameters.AddWithValue("@ProjectId", querystring);
ArrayList emailArray = new ArrayList();
reader = myCommand.ExecuteReader();
Dictionary<string, string> emails = new Dictionary<string, string>();
while (reader.Read())
{
emails[Convert.ToString(reader["email"])] = Convert.ToString(reader["FirstName"]);
}
foreach (string email in emails.Keys)
{
//email codes
}
}
这就是我试过的
public class EmailCode
{
public string Email { get; set; }
public string FirstName { get; set; }
public string Program { get; set; }
}
protected void emailtest_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataReader reader;
string sendMessage = "SELECT aspnet_Membership.Email, User_Profile.FirstName, User_Profile.Program FROM aspnet_Membership join User_Profile on User_Profile.UserId = aspnet_Membership.UserId";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(sendMessage, myConnection);
ArrayList emailArray = new ArrayList();
reader = myCommand.ExecuteReader();
var emails = new List<EmailCode>();
while (reader.Read())
{
emails.Add(new EmailCode
{
Email = Convert.ToString(reader["email"]),
FirstName = Convert.ToString(reader["FirstName"]),
Program = Convert.ToString(reader["Program"])
});
}
foreach (EmailCode email in emails)
{
const string username = "";
const string password = "
SmtpClient smtpclient = new SmtpClient();
MailMessage mail = new MailMessage();
MailAddress fromaddress = new
smtpclient.Host = "";
smtpclient.Port = 25;
mail.From = fromaddress;
mail.To.Add(Email);
mail.Subject = ("");
//mail.Attachments.Add(new mail);
mail.IsBodyHtml = true;
mail.Body = Hello" + HttpUtility.HtmlEncode(FirstName) + " you are studying " + HttpUtility.HtmlEncode(Program) + ";
smtpclient.EnableSsl = false;
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
smtpclient.Send(mail);
}
答案 0 :(得分:0)
您有两种选择。您可以一起停止使用字典,也可以使用字典,其值为类,例如Tuple
。以下是使用新类型的示例。更新SQL查询以返回Program
数据后,您可以执行以下操作。
//Create a new class for our Poco
public class EmailCode
{
public string Email {get; set;}
public string FirstName {get; set;}
public string Program {get;set;}
}
var emails = new List<EmailCode>();
while (reader.Read())
{
emails.Add(new EmailCode {Email = Convert.ToString(reader["email"]),
FirstName = Convert.ToString(reader["FirstName"]),
Program = Convert.ToString(reader["Program"])
});
}
foreach (EmailCode email in emails)
{
//email codes
}
在你的&#34;我尝试了什么&#34;编辑,你需要访问从循环中公开的电子邮件变量的属性,像这样(更不用说你有语法错误):
mail.Body = "Hello" + HttpUtility.HtmlEncode(email.FirstName) + " you are studying " + HttpUtility.HtmlEncode(email.Program);
smtpclient.EnableSsl = false;
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
smtpclient.Send(mail);