Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
it wait at
C#
client.Send(mail);
and give exception

C#
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at check_email.Email.Unnamed_Click(Object sender, EventArgs e) in c:\users\anwar ali\documents\visual studio 2015\Projects\check_email\check_email\Email.aspx.cs:line 


What I have tried:

C#
string pweda = "password"; //(ConfigurationManager.AppSettings["password"]);
string from = "from@gmail.com"; //Replace this with your own correct Gmail Address
string to = "to@gmail.com"; 
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress(from);
            mail.Subject = "This is a test mail";
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = "Test Mail.";

            mail.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();

            //Add the Creddentials- use your own email id and password
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(from, pweda);
            client.Port = 587; // Gmail works on this port
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Gmail works on Server Secured Layer

            try
            {
                client.Send(mail);
                Response.Write("Message Sent...");
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty;
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }
                HttpContext.Current.Response.Write(errorMessage);
            }
Posted
Updated 19-May-16 6:01am
Comments
Have you used the correct gmail id and password?
Danyal Awan 13-May-16 6:34am    
yes
Do you have 2 step verification on in Gmail?
Danyal Awan 13-May-16 6:58am    
No
Try with some other email account and see if it is working or not.

 
Share this answer
 
Comments
Danyal Awan 13-May-16 6:58am    
I have tried this code but it give error

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

at line smtp.Send(mm);
login to gmail, open new tab with this url Gmail support via Apps.[^] -> Click - Turn On
Now run your code, it should work.

when i came accorss this issue, i just remembered this post and answered to it.
 
Share this answer
 
Comments
Karthik_Mahalingam 19-May-16 12:21pm    
alternate url for allowing gmail from Non Google Apps: Sign in - Google Accounts[^]

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