0

Hi guys I create a mail class to send emails.

public void SendEmail(string subject, string messageBody, string toAddress)
{
    MailMessage mail = new MailMessage();
    mail.To.Add(toAddress);
    //mail.To.Add("amit_jain_online@yahoo.com");
    mail.From = new MailAddress("noreply3ncra@gmail.com");
    mail.Subject = subject;
    string Body = messageBody;
    mail.Body = Body;
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.UseDefaultCredentials = false;

    smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
    smtp.Credentials = new System.Net.NetworkCredential
         ("noreply3ncra@gmail.com", "********");
    //Or your Smtp Email ID and Password
    smtp.EnableSsl = true;
    smtp.Send(mail);
}

But I got this error:

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

So I have to sign into Gmail and enter the captcha code and after this every thing is going to be ok.

What should I do?

Adam Zuckerman
  • 1,633
  • 1
  • 14
  • 20
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

2 Answers2

2

Try this:

You should enable application to access gmail account.

Try this link:

Gmail allow access

For more info refer this link:

Stackoverflow link

Community
  • 1
  • 1
Vishal I P
  • 2,005
  • 4
  • 24
  • 41
2

Add this also:

Add port no also to your code as:

 smtp.Port = 587;

For more info refer this link:

Codeproject link