-1

I am trying to send send email through below action method

    [HttpPost]
    public ActionResult ForgotPassword1()
    {
        dbAlKhaleejEntities _context = new dbAlKhaleejEntities();
        var email = Request["Email"];
        var email_adress = _context.CUSTOMERs.First(em => email == em.CUSTOMER_EMAIL);
        var mailto = email_adress.CUSTOMER_EMAIL;

            MailMessage msg = new MailMessage();

            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.Credentials = new NetworkCredential("umairliaquat63@gmail.com", "abc");
            client.Host = "smtp.gmail.com";
            client.Port = 587;

            msg.From = new MailAddress("umairliaquat63@gmail.com");
            msg.To.Add(mailto);
            msg.Subject = "Password recovery";
            msg.Body = "Test Recovering the password";
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;


            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;

            client.Send(msg);
            return RedirectToAction("forgetPassword");
  }

but at line "client.Send(msg)" , it is throwing exception "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required". How I should solve this problem

Umair
  • 55
  • 5

2 Answers2

1

As far as I know you don't need write your own code cause SmtpClient can use your configuration:

<system.net>
  <mailSettings>
    <smtp from="It's me &lt;its@my.email&gt;">
      <network host="smtp.ip.or.domain" port="527" defaultCredentials="false" userName="noreply" password="12345" enableSsl="true"/>
    </smtp>
  </mailSettings>
</system.net>

Verify your mail settings twice. In addition, the SMTP server can reject connections for IP's from a black list, or not from a white list. The SMTP server can detect your mail as a spam.

You can use your own local SMTP or specialized servises to mass mailings.

Mark Shevchenko
  • 7,297
  • 1
  • 21
  • 25
0

Try this snippet :

 using (var client = new SmtpClient(SmtpServerHost, SmtpPort)
  {
    Credentials = new NetworkCredential(NetworkCredentialUserName, Password),
    EnableSsl = this.enableSSL
   }
   )
    {
      MailMessage msg = new MailMessage();          
      msg.From = new MailAddress(FromMailingAddress);
      msg.Subject = this.Subject;
  }
Nikitesh
  • 1,239
  • 1
  • 16
  • 34