Click here to Skip to main content
15,925,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when I try to send mail then I receive error please give me solution thanking you
Gmail Error :
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required


C#
msg = new MailMessage();
           SmtpClient smtp = new SmtpClient();
           emailId = txtEmailId.Text.Trim();
           //sender email address
           msg.From = new MailAddress("yaduvir4@gmail.com");
           //Receiver email address
           msg.To.Add(emailId);
           msg.Subject = "Confirmation email for account activation";
           //For testing replace the local host path with your lost host path and while making online replace with your website domain name
           ActivationUrl = Server.HtmlEncode("http://localhost:8665/MySampleApplication/ActivateAccount.aspx?UserId=" + FetchUserId(emailId) + "&EmailId=" + emailId);

           msg.Body = "Hi " + txtName.Text.Trim() + "!\n" +
                 "Thanks for showing interest and registring in <a href='http://www.webcodeexpert.com'> webcodeexpert.com<a> " +
                 " Please <a href='" + ActivationUrl + "'>click here to activate</a>  your account and enjoy our services. \nThanks!";
           msg.IsBodyHtml = true;
           smtp.Credentials = new NetworkCredential("sendfrommailaddress.com", "password");
           smtp.Port = 587;
           smtp.Host = "smtp.gmail.com";
           smtp.EnableSsl = true;
           smtp.Send(msg);
           clear_controls();
           ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Confirmation Link to activate your account has been sent to your email address');", true);
Posted
Updated 21-Nov-15 6:05am
v2
Comments
Suvendu Shekhar Giri 21-Nov-15 12:11pm    
make sure that credentials are correct.

Possible reasons-
1. Incorrect Username and/or password
First you must check whether the Username and Password supplied are correct.
Though Gmail allows you to sign in with Username instead complete Email but when sending email through code you need to pass the complete Email address as Username.

2. SSL setting
Gmail uses secure channel for sending emails and you need to enable SSL security while sending emails through your application
In .Net it can be enabled by setting EnableSsl setting of the SmtpClient to true.

3. Less Secure Apps setting
If you are sure that your Username and Password are correct, but you still get errors then it means that Gmail has blocked your application.

If the reasons #1, #2 and #3 are not working for you then it might be the issue with the location of the Application accessing the Gmail account.

[Ripped from here][^]

-KR
 
Share this answer
 
Either your password is incorrect or you have activated two step verification. If the second case is true, then you need to generate a App Password, which you can use here instead of this login password.

Check my pas answer for a fully working code after you resolve these issues - sending email to gmail from asp.net[^]

Also I guess you forgot this line...
C#
smtp.UseDefaultCredentials = false;
 
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