Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Below is the code I am using for sending mail from my local. I have WCF class library project where this code is written. I am calling this function from ASP.net website.

I am getting error :

"Mail not sent : 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"

Code in c#.net 3.5 :

public String SendMail()
{

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com", "Email head", System.Text.Encoding.UTF8);
mail.Subject = "This mail is send from asp.net application";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("from@gmail.com", "password");
client.Port = 587;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 100000;

try
{
client.Send(mail);
return "Mail sent";
}
catch (Exception ex)
{
return "Mail not sent : " + ex.Message.ToString();
}
}

I have done all below settings in my from@gmail.com account.

2-Step Verification Disabled
Access for less secure apps Enabled

Forwarding and POP/IMAP -> Enable POP for all mail (even mail that's already been downloaded)

Forwarding and POP/IMAP -> Enable IMAP
Posted
Comments
TheRealSteveJudge 4-Dec-14 6:47am    
Just an idea: Can you try port 465?
I found it at: http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm
Richard MacCutchan 4-Dec-14 8:24am    
I use 587 and it works fine.
forccyy 5-Dec-14 0:43am    
With port no - 465 I am getting "Operation result time out error".
Richard MacCutchan 4-Dec-14 8:23am    
The message from the server is telling you what the problem is. Check the userid and password are valid for gmail.
forccyy 5-Dec-14 0:43am    
Thank u for reply. But I have already verified multiple times. and user name password seems to be correct.

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