Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send an email in asp.net
i am accessing smpt ,username...atc parameters from web.config file.
my all credentials are correct but each time i get this 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


my code is this

C#
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
        {
            try
            {
                using (System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage())
                {
                    Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
                    MailSettingsSectionGroup mailSettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

                    mailMessage.From = new MailAddress(mailSettings.Smtp.From);
                    mailMessage.Subject = subject;
                    mailMessage.Body = body;
                    mailMessage.IsBodyHtml = true;
                    mailMessage.To.Add(new MailAddress(recepientEmail));
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = mailSettings.Smtp.Network.Host;
                    smtp.EnableSsl = mailSettings.Smtp.Network.EnableSsl;
                    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                    NetworkCred.UserName = mailSettings.Smtp.Network.UserName;
                    NetworkCred.Password = mailSettings.Smtp.Network.Password;
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = mailSettings.Smtp.Network.Port;
                    smtp.Send(mailMessage);
                    
                }
            }




in config file
<system.net>
    <mailsettings>
      <smtp deliverymethod="Network" from="pearlcontinentalhb@gmail.com">
        <network host="smtp.gmail.com" username="Pearl Continental Hotel ,Bhurban" password="pchb" port="587" enablessl="true" />
      </smtp>
    </mailsettings>
  </system.net>
Posted
Updated 19-Jul-14 1:17am
v4

1 solution

set
smtp.UseDefaultCredentials = false;
when you are not using default credentials.

see here: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.usedefaultcredentials%28v=vs.110%29.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900