Click here to Skip to main content
15,903,540 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all!!

i am trying to send mail through asp.net but it gives me error. i googled it but found answers related to networking and i had no idea what to do. Plz help me out.

it gives me the following error---

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.


Here is the code of my .aspx.cs

C#
protected void btnSubmitt_Click(object sender, EventArgs e)
   {
       MailMessage mail = new MailMessage();
       mail.Subject =  "Enquiry From the User Of the WebSite";
       mail.Body = "Dear sir , <br/> Following user has provided a feedback about your website <br/><br/>" + "<b>Name :</b>" + txtName.Text + "<br/><b>Email Id :</b>" + txtEmailId.Text + "<br/><b>Mobile No :</b>" + txtMobileNo.Text + "<br/><b>Feedback:</b>" + txtEnquiry.Text;
     
       mail.From = new System.Net.Mail.MailAddress("arcwebsolutions@gmail.com", "Feedback From Website");
       mail.IsBodyHtml = true;
      
       mail.To.Add("ujjwal.uniyal111@gmail.com");
       
       NetworkCredential cred = new NetworkCredential("arcwebsolutions@gmail.com", "password");
      
       SmtpClient smtp = new SmtpClient("smtp.gmail.com");
       smtp.UseDefaultCredentials = false;
       smtp.EnableSsl = true;
       
       smtp.Credentials = cred;
       smtp.Port = 587;
       
       smtp.Send(mail);
       clear();
   }
Posted
Updated 12-Dec-11 9:24am
v3
Comments
Richard Arias Prospero 14-Dec-11 10:33am    
Thank you! Helped me a lot. I'm currently using VB instead of C#. But it was easy to convert between them when you know them as well.

Try using the following code, hope it will work. Change credentials (UserName & Password) and receiver address according to yours.

C#
SmtpClient client = new SmtpClient();
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      client.EnableSsl = true;
      client.Host = "smtp.gmail.com";
      client.Port = 587;

      // setup Smtp authentication
      System.Net.NetworkCredential credentials =
          new System.Net.NetworkCredential("gmailusername@gmail.com", "gmailpassword");
      client.UseDefaultCredentials = false;
      client.Credentials = credentials;

      MailMessage msg = new MailMessage();
      msg.From = new MailAddress("gmailusername@gmail.com");
      msg.To.Add(new MailAddress("xyz@gmail.com"));
      msg.Subject = "This is a test Email subject";
      msg.IsBodyHtml = true;
      msg.Body = string.Format("<html><head></head><body>Test HTML Email</body>");
      try
      {
          client.Send(msg);
          //lblMsg.Text = "Your message has been successfully sent.";
      }

      catch (Exception ex)
      {
          //lblMsg.ForeColor = Color.Red;
          //lblMsg.Text = "Error occured while sending your message." + ex.Message;

      }


Make sure you have removed all mail settings from your web.config file.

Let me know if there is any issue.
 
Share this answer
 
v2
Comments
RaviRanjanKr 12-Dec-11 13:33pm    
Nice Answer, My 5!
Monjurul Habib 12-Dec-11 13:34pm    
thank you
Uday P.Singh 12-Dec-11 14:52pm    
5ed :)
Monjurul Habib 12-Dec-11 14:53pm    
Thank you, Uday
[no name] 12-Dec-11 20:18pm    
5!
Have a look here

http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp[^]

The answer regarding password strength may well be your issue.

I suggest you change you password ASAP, since you've just posted it on a public forum!!
 
Share this answer
 
Comments
ujjwal uniyal 12-Dec-11 7:30am    
thanks for the advise sir but i posted fake password here.. :)
ujjwal uniyal 12-Dec-11 7:33am    
I tried the solution provided by you sir but it didn't worked... :(
Monjurul Habib 12-Dec-11 15:19pm    
did you try my code ?? hope it will work.

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