1

I have the following action method to send emails using gmail smtp:-

   [HttpPost]
            public ActionResult Contact(Contact c)
        {

            //
            if (ModelState.IsValid)
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("*****");
                mail.To.Add(new MailAddress("******"));
                mail.Subject = "New Feedback from Edama Web Site";
                mail.IsBodyHtml = true;
                System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
                mailBody.AppendLine("<b>Dear Sirs, </b><br/><br/>");

                mail.Body = mailBody.ToString();
                // Create a new Smpt Client using Google's servers
                var mailclient = new SmtpClient();
                mailclient.Host = "smtp.gmail.com";
                mailclient.Port = 587;

                // This is the critical part, you must enable SSL
                mailclient.EnableSsl = true;
                mailclient.Credentials = new System.Net.NetworkCredential
                     ("***", "***"); /

                //Or your Smtp Email ID and Password
                //smtp.EnableSsl = true;
                mailclient.Send(mail);




                TempData["message"] = string.Format("Your Feedback has been submitted. Thanks");
                return RedirectToAction("Contact");
            }
            return View(c);

        }

now when i am running my web application under VS 2013 everything will work well , where i will receive emails correctly. but now i deploy my web application under a hosting server , and when i call the above action method i got 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. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: 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

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

so what is causing this error ?

second question. i am a bit confused , as inside my action method i define the following mailclient.EnableSsl = true; does this mean that my web application should be running under https which is not my case, as my web application is a public web site so it is not under https .. and as i mentioned before when i call my action method inside VS 2013 on for example localhost:1234 the smtp send email correctly even i am not running my localhost under https ...

  • @Taleeb if I change the port number to be 465 I will get this exception on even when running my application from VS @- Unable to read data from the transport connection: net_io_connectionclosed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. Source Error: Line 196: mailclient.Send(mail); –  Sep 02 '15 at 23:18

1 Answers1

1

When using ssl over gmail smtp - the port number used in 465. Src: https://support.google.com/a/answer/176600?hl=en

Enable SSL property while sending email does not require your application to run over ssl. Rather it specifies whether SSL is used to access the specified SMTP mail server.

Taleeb
  • 1,888
  • 18
  • 25
  • now I change the port from 587 to be 465 , but I am receiving this exception even when I run the project inside Visual studio :- Unable to read data from the transport connection: net_io_connectionclosed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException:"......... while using 587 will work when running my project on VS but will not work wen I host my application to a hosting provider –  Sep 02 '15 at 23:21
  • 1
    Try by setting mailClient.UseDefaultCredentials = false. Also enable telnet and try "telnet smtp.gmail.com 465" from command prompt – Taleeb Sep 03 '15 at 06:27
  • this did not work either. I added "mailclient.UseDefaultCredentials = false;" and I tried using port 587 & port 465 . also if I use port 465 then even sending email will not work when I run the project inside visual studio , where I will get this exception "An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code Additional information: The operation has timed out." –  Sep 03 '15 at 22:32
  • According to an answer here (http://stackoverflow.com/questions/26113404/problems-sending-e-mail-in-web-app) - you need to set smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; for gmail smtp to work. Also are you getting any email from google that it is blocking or something? – Taleeb Sep 04 '15 at 09:02
  • I tied adding "smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network" which again worked well if I run my application inside VS , but if I try this on my hosted application I will get this error ... –  Sep 04 '15 at 23:32
  • T"he SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Net.Mail.SmtpException: 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" –  Sep 04 '15 at 23:32