Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
MailMessage msg = new MailMessage();
SmtpClient client = new SmtpClient("smtp.gmail.com",587);
client.EnableSsl = true;        
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("myid@gmail.com","mypassword");
client.DeliveryMethod = SmtpDeliveryMethod.Network;    
msg.To.Add(new MailAddress("toid@yahoo.co.in"));
msg.From = new MailAddress("myid@gmail.com");
msg.Subject = "Test Mail";
msg.Body = "Body Of the E-mail";
try
{
    client.Send(msg);
    MessageBox.Show("Sent");
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex);
    this.Dispose();
}


this is my code. can someone tell me what is wrong with this. i am getting error 5.5.1 AUTHENTICATION REQUIRED.
Further Details of error-The SMTP server requires a secure connection or the client was not authenticated.

i entered the correct username and password while testing but it's not working.
plz don't give links to other similar pages, i have already spent 6 hrs looking at them.
thanks in advance
Posted
Updated 2-Mar-17 19:57pm
v2
Comments
Sergey Alexandrovich Kryukov 31-Jan-12 15:09pm    
Hard to say, it looks fine... only, 1) are you sure the server uses SSL? 2) did you try to do the same using some available e-mail sending software and exact same authentication options? Did it work?

--SA

Your code is absolutely correct.
I tried your code with two different gmail accounts.
With the first account the message was sent. By using the second one I could reproduce your exception.
The difference with these two accounts is that the first one is using the plain authentication while the second one is using Google's two-way authentication.
I'm pretty sure the last scenario applies here ;)

Kindly try to add a new application specific password (in your gmail account settings) and use this one instead of your regular account password within your application.
 
Share this answer
 
v2
Comments
suvam8694 1-Feb-12 4:03am    
thanks a lot, it worked. yes my account uses the two step verification.
so thanks again.
Goto Account of Gmail , then select Connected apps & sites Allow less secure apps: ON(if this is off you cannot send mails through apps,or your websites )
 
Share this answer
 
Comments
Graeme_Grant 3-Mar-17 2:04am    
Any reason why you are replying to a 5 year old question?

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