在ASP.net中单击按钮后发送电子邮件C#

时间:2014-12-29 20:23:44

标签: c# asp.net asp.net-mvc

您好我想在按钮点击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)

它没有出现任何错误,我也没有收到任何电子邮件

1 个答案:

答案 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帐户中关闭安全设置。

编辑:就像我说你应该关闭gmail帐户中的安全设置!如果这个链接没有帮助你只是谷歌例外!检查Link!或者LINK