Click here to Skip to main content
15,940,246 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to send email using gmail, below is my code

SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("from@gmail.com", "From Me");
MailAddress toAddress = new MailAddress("to@gmail.com", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.Host = "smtp.gmail.com";
smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");

smtpClient.EnableSsl = true;


smtpClient.Port = 587;
   
smtpClient.Send(message);




I am getting this error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
Password and username is all correct, i tried lot of things but not working :(

Plz anyone help me

Thanks
Posted
Updated 17-Mar-11 19:40pm
v2

Hope this[^] might help you.
 
Share this answer
 
Hi,

You did not specified Smtpclient name in your SmtpClient() object.
Just add this line above the declaration of new SmtpClient(); object.

String server= "smtp.gmail.com";

and pass the string parameter to smtpclient object like this :
SmtpClient smtpClient = new SmtpClient(server);

instead of "SmtpClient smtpClient = new SmtpClient();"

this will work.. :)
 
Share this answer
 
Comments
ProgrammerAt 18-Mar-11 2:11am    
Hi Kapil,
your code worked :) thanks a lot.

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