Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In Asp.net c#   i have  created a web app which sends mail to desired destination  and this thing works from localhost ,my live project is  uploaded on solidcp panel and from that  live website my mail sending function does not work.I also allowed less secure apps from gmail settings but still not working.
Is there something i should configure from solidcp panel or my code needs some changes?


What I have tried:

public void SendMail(string strTO, string strSubject, string strBody, FileUpload objFileUpload = null)
  {
      try
      {

          tblSettings objSettings = new tblSettings();
          objSettings.LoadAll();

          if (objSettings.RowCount > 0)
          {
              string strFrom = objSettings.AppSiteEmail;
              //"rahul.b4r097@gmail.com""sanikp@keyconcepts.co.in";
              if (strTO == "")
              {
                  strTO = objSettings.AppRecepientEmail;
              }
              MailMessage mail = new MailMessage(strFrom, strTO, strSubject, strBody);
              mail.IsBodyHtml = true;


              SmtpClient smtp = new SmtpClient();
              smtp.Host = objSettings.AppSMTP;
              //"smtp.gmail.com"

              if (!string.IsNullOrEmpty(objSettings.s_AppPortNumber))
              {
                  smtp.Port = Convert.ToInt32(objSettings.AppPortNumber.ToString());
                  //587
              }

              smtp.UseDefaultCredentials = false;
              smtp.Timeout = 60000;

              clsEncryption objEncrypt = new clsEncryption();

              smtp.Credentials = new System.Net.NetworkCredential(strFrom, objEncrypt.Decrypt(objSettings.AppEmailPassword, appFunctions.strKey));
              if (objSettings.s_AppIsSSL != "")
              {

                  smtp.EnableSsl = Convert.ToBoolean(objSettings.AppIsSSL);
              }
              else
              {
                  smtp.EnableSsl = false;
              }
              var str = objEncrypt.Decrypt(objSettings.AppEmailPassword, appFunctions.strKey.ToString());
              string exe = " MailTO: " + strTO + " ,MailFrom: " + strFrom + " ,Subject :" + strSubject +  " ,Host: " + smtp.Host + " ,Port: " + smtp.Port + " ,SSL: " + smtp.EnableSsl+ ",appSiteEmail: "+ objSettings.AppSiteEmail+ " ,appRecipientEmail "+ objSettings.AppRecepientEmail +  " ,Password: "+str ;
              LogError(exe);
              smtp.DeliveryMethod = SmtpDeliveryMethod.Network;


              smtp.Send(mail);

              LogError(strTO);

              smtp = null;
              mail = null;
          }
      }
      catch (Exception ex)
      {
          string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
          message += Environment.NewLine;
          message += "-----------------------------------------------------------";
          message += Environment.NewLine;
          message += string.Format("Message: {0}", ex.Message);
          message += Environment.NewLine;
          message += string.Format("StackTrace: {0}", ex.StackTrace);
          message += Environment.NewLine;
          message += string.Format("Source: {0}", ex.Source);
          message += Environment.NewLine;
          message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
          message += Environment.NewLine;
          message += "-----------------------------------------------------------";
          message += Environment.NewLine;
          string path = HttpContext.Current.Server.MapPath("~/Error/ErrorLog1.txt");
          // Check if file already exists. If yes, delete it.
          if (!File.Exists(path))
          {
              File.Delete(path);
              var myFile = File.Create(path);
              myFile.Close();
              System.IO.File.WriteAllText(path, message);

              myFile.Close();
              return;

          }


          if (File.Exists(path))
          {
              File.Delete(path);
              var myFile = File.Create(path);
              myFile.Close();
              System.IO.File.WriteAllText(path, message);

              myFile.Close();
              return;
          }










      }

  }
Posted
Comments
Michael_Davies 6-Apr-22 10:00am    
Hard to say what may be the issue as the parameter contents are not visible, hard code the parameters to test, so set the host, port etc. hard values and test until success.

Try port 465 with SSL. If you use port 587 or 465 then SSL=true. Google uses port 465 for SSL, 587 is Start/TLS and non-secured is port 25.

You say no to standard credentials, however credentials are required if the server requires the client to authenticate before it will send email on the client's behalf and secure ports do require authentication.

If the account has 2fa enabled then you may have to generate and use an App password. When you log in to Gmail for the first time on a device Gmail will challenge the log in, once logged in the device is recognised and generally will not challenge again (2fa gives the option to never ask again on the device). This may be why it works locally but not on your server, locally you have logged in so Gmail allows the traffic whereas on your server Gmail is challenging and not getting a response and drops the requests.

Take a look at https://www.c-sharpcorner.com/UploadFile/009464/send-email-to-gmail-in-Asp-Net-using-C-Sharp/
Richard Deeming 7-Apr-22 4:48am    
REPOST
You have already posted this question:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 authentication required.[^]

If you don't get an answer, don't post the same question again. Instead, go back to the original question and edit it to provide additional information.

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