在 C# 中单击按钮后发送电子邮件 ASP.net
本文关键字:电子邮件 ASP net 单击 按钮 | 更新日期: 2023-09-27 18:35:33
嗨,我正在尝试在按钮单击 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)
它没有出现任何错误,也没有收到任何电子邮件
这段代码应该有效:
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帐户中关闭安全设置。
编辑:就像我说的,你应该关闭Gmail帐户中的安全设置!如果此链接不能帮助您,只需谷歌异常!检查链接 !或者这个链接