我有此代码可在按钮单击后发送电子邮件。在查询中使用多个值在asp.net中发送电子邮件
我想在查询中使用aspnet_Users.UserName作为电子邮件的一部分,但它不起作用。
以下是我的代码。
谢谢
SqlDataReader reader;
string sendMessage = "SELECT aspnet_Membership.Email, aspnet_Users.UserName, User_Profile.FirstName FROM aspnet_Membership INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN User_Profile ON aspnet_Users.UserId = User_Profile.UserId WHERE (aspnet_Membership.UserId = @UserId)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(sendMessage, myConnection);
myCommand.Parameters.AddWithValue("@UserId", newUserId);
ArrayList emailArray = new ArrayList();
reader = myCommand.ExecuteReader();
while (reader.Read())
{
emailArray.Add(reader["email"]);
string name = reader["UserName"].ToString();
}
foreach (string email in emailArray)
{
//send new confirmation email
const string username = "------------";
const string password = "------------";
SmtpClient smtpclient = new SmtpClient();
MailMessage mail = new MailMessage();
MailAddress fromaddress = new MailAddress("-------------", "--------");
smtpclient.Host = "---------";
smtpclient.Port = 25;
mail.From = fromaddress;
mail.To.Add(email);
mail.Subject = ("Welcome to -------);
//mail.Attachments.Add(new mail);
mail.IsBodyHtml = true;
mail.Body =
//the message that goes in the body of the email
"+ HttpUtility.HtmlEncode(name) + ";
smtpclient.EnableSsl = false;
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
smtpclient.Send(mail);
}
reader.Close();
myConnection.Close();
}
你从来不会在'while'块中用'username'做任何事情。 – mason 2015-02-06 20:15:37
@mason thaks,你建议我用while循环中的用户名做什么? – Musa 2015-02-06 20:35:03