您好我想在按钮点击Asp.net后发送电子邮件。
将从查询中检索发送电子邮件的电子邮件。
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataReader reader;
string sendMessage = "SELECT aspnet_Membership.Email FROM aspnet_Membership join User_Profile on User_Profile.UserId = aspnet_Membership.UserId JOIN Project_List on Project_List.ProfileId = User_Profile.ProfileId WHERE Project_List.ProfileId = 1";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(sendMessage, myConnection);
ArrayList emailArray = new ArrayList();
reader = myCommand.ExecuteReader();
while (reader.Read())
{
emailArray.Add(reader["email"]);
}
foreach (string email in emailArray)
它没有出现任何错误,我也没有收到任何电子邮件
答案 0 :(得分:1)
此代码应该有效:
foreach(string email in emailArray)
{
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("tayyib@gmail.com", "xxxxxxxxx");
smtp.EnableSsl = true;
MailMessage msg = new MailMessage("tayyib@gmail.com", email);
msg.Subject = "Test1";
msg.Body = "Test2";
smtp.Send(msg);
}
您应该在Gmail帐户中关闭安全设置。