Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys!

I got an error in this part saying, "The SMTP server requires a secure connection or the client was not authenticated. " the browser is pointing at
smtp.Send(fromAddress, toAddress, subject, body);
. It is where the error. But I think it is not.

Here is my code:
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
            
            GeneratePass(); //Generate System ID
            s = WebConfigurationManager.ConnectionStrings["amicassa"].ConnectionString;
            amicasaCon = new SqlConnection(s);
            amicasaCon.Open();

            SqlCommand cadd = new SqlCommand("Update users set system_id ='"+oldpassword.Text+"' WHERE username = '"+username.Text+"' ", amicasaCon);
            SqlDataReader rcd = cadd.ExecuteReader();
            rcd.Close();
            amicasaCon.Close();
            try
            {
                //here on button click what will done 
            SendMail();
            newpassword.Text = "";
            oldpassword.Text = "";
            Response.Redirect("ChangePassword.aspx");
            
        }
        catch (Exception) { }

       
    }

    protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = "xxxxx@gmail.com";
        // any address where the email will be sending
        var toAddress = newpassword.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "xxxxxxxx";
        // Passing the values and make a email formate to display
        string subject = oldpassword.Text.ToString();
        string body = "From: ADMIN "+"\n";
        body += "Email: " + newpassword.Text + "\n";
        body += "System ID : " + oldpassword.Text + "\n";
        body += "Please DO NOT REPLY" + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }
Posted

 
Share this answer
 
Comments
DarkDreamer08 28-Aug-14 1:25am    
I fixed now the error. But I got a problem. It is just executing the query then it does not move to the next function which is to send mail after I press the image button. What should I do? :(
Sergey Alexandrovich Kryukov 28-Aug-14 1:40am    
Not clear. What query, what "next function"? Isn't it a different question?
Did I answer your present question? Will you formally accept it?
—SA
DarkDreamer08 28-Aug-14 1:43am    
it is about the above code. The query inside the Image button Event Handler. the next function I am pertaining is the SendMail() which is inside the try {}and catch{}
Sergey Alexandrovich Kryukov 28-Aug-14 1:47am    
To start with, don't block propagation of exceptions with "catch (...) {}". You try-catch too locally. Remove try-catch at all from the handler. You should try up the stack, in this case, it could be the main event loop.
—SA
DarkDreamer08 28-Aug-14 1:55am    
I just remove the try-catch. But the code inside the try-catch remains because I am calling it after the query executes fine. Now, an error displayed, "The operation has timed out.Error in smtp.Send(fromAddress, toAddress, subject, body);"
 
Share this answer
 
It seems that your current SMTP settings are not OK.
In your case for SSL Enabled the STMP port should be: 465

You could see details about sending email by using "gmail" settings problems and solutions at the next links:
https://support.google.com/mail/answer/78775?hl=en[^]
 
Share this answer
 
Comments
DarkDreamer08 28-Aug-14 1:24am    
I fixed now the error. But it does not send any message to the email that I put. It just doing the first condition which is updating the system ID in the database then nothing happens next whenever I press the image button. What should I do?
Raul Iloc 28-Aug-14 1:29am    
1.Please check if the email is not in the SPAN list, or maybe the email will come after a delay.
2.You should try first to test your code by sending email to another existing gmail account.
DarkDreamer08 28-Aug-14 1:33am    
actually, it works fine yesterday when I followed the steps you gave to me on the post about sending email yesterday. So, today, I just add a query to update first the system ID generated by the system to the database before it send to the user's email. When I run the program, it does not send, but it executes the query fine.
Raul Iloc 28-Aug-14 1:47am    
Then, if you have the code from yesterday before your new changes, the sending email part should work also today, because accessing the database is not related with the sending email, but check if the data read from there is valid, maybe is a wrong value for email address read from the database or something. So you should inspect your code by using Debug during execution.
DarkDreamer08 28-Aug-14 1:50am    
The code above is the code you taught to me yesterday. Exactly as it is. I just included there the codes for database.
 
Share this answer
 
Comments
DarkDreamer08 27-Aug-14 23:02pm    
I have read that and followed to. same error displays. That is why I switched back to my original code that I posted above. To be honest, yesterday, it works fine. but today, when I add some codes specifically the query, it does not work :(
C#
public static Boolean SendingMail(string From, string To, string Subject, string Body)
    {
        
            try
            {
                MailMessage m = new MailMessage("Uma<test@gmail.com>", To);
                m.Subject = Subject;
                m.Body = Body;
                m.IsBodyHtml = true;
                m.From = new MailAddress(From);
 
                m.To.Add(new MailAddress(To));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
              
                NetworkCredential authinfo = new NetworkCredential("test@gmail.com","password");
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = authinfo;
                smtp.Send(m);
                return true;
 
               
 

            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
Share this answer
 
Comments
Gihan Liyanage 15-Sep-14 6:23am    
Did you got a solution from my support, I can see you have not accepted any answer.If you are ok with this answer plz accept it. Then any user having same problem can identified it has solved the problem..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900