Send Email After Button Click in ASP.net C#

Hi i am trying to send am email after Button click in Asp.net.

The emails to send the email to will be retrieved from a query.

        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)

it doesn’t come up with any errors, and i don’t receive any emails

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This code should work:

foreach(string email in emailArray)
{
     SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
     smtp.UseDefaultCredentials = false;
     smtp.Credentials = new NetworkCredential("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9cdd8c0c0d0dbf9ded4d8d0d597dad6d4">[email protected]</a>", "xxxxxxxxx");
     smtp.EnableSsl = true;

     MailMessage msg = new MailMessage("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcbdec6c6d6ddffd8d2ded6d391dcd0d2">[email protected]</a>", email); 
     msg.Subject = "Test1";
     msg.Body = "Test2";

     smtp.Send(msg);
 }

There is security setting which you should turn off in your gmail account.

EDIT: Like I said you should TURN OFF security setting in gmail account ! If this link doesn’t help you just google the exception ! Check the Link ! Or this LINK


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x