Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
below error is shown when sent the mail

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 


What I have tried:

I use below code to mail config .thats works in other applications but my application shows the below error how to fix it.

Msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["MailServer"];
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
Msg.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"]);
NetworkCred.Password = ConfigurationManager.AppSettings["MailPassword"];
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.EnableSsl = true;

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};

smtp.Send(Msg);
Posted
Updated 27-Apr-17 5:55am
v2
Comments
F-ES Sitecore 26-Apr-17 11:45am    
Try

smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
Andy Lanng 26-Apr-17 12:51pm    
OP gone again? that's why so few solutions highlight O_o
Raja Ganapathy 27-Apr-17 1:25am    
I have use this also but the same error is occurs
David_Wimbley 27-Apr-17 2:33am    
Are you sure the port number is correct? Typically mail servers will have a different SMTP portal for SSL/TLS connections. For example office365 uses port 587.

1 solution

You've set the NetworkCred.Password, but not the NetworkCred.UserName. Most servers will require both a username and a password for authentication.

But there's a better way: put your configuration in the <smtp> Element (Network Settings)[^]:
XML
<configuration>  
  <system.net>  
    <mailSettings>  
      <smtp deliveryMethod="network" from="example@gmail.com">  
        <network  
          host="smtp.gmail.com"
          port="587"
          enableSsl="true"
          defaultCredentials="false" 
          userName="YOUR-USER-NAME" 
          password="YOUR-PASSWORD"
        />  
      </smtp>  
    </mailSettings>  
  </system.net>  
</configuration> 

With that in place, you don't need to configure the SmtpClient; the default constructor will automatically read the settings from the config file.
C#
SmtpClient smtp = new SmtpClient();
smtp.Send(Msg);
 
Share this answer
 

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