Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.



My project is uploaded to a remote server where it sends this particular error but it runs at local host for about sending mails.I also allowed less secure app on the gmail account but still not able to understand.

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 = objSettings.AppIsSSL;
                }
                else
                {
                    smtp.EnableSsl = false;
                }
                string exe = " MailTO: " + strTO + " ,MailFrom: " + strFrom + " ,Subject :" + strSubject + " ,Body: " + strBody + " ,Host: " + smtp.Host + " ,Port: " + smtp.Port + " ,SSL: " + smtp.EnableSsl;
                LogError(exe);

                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
Richard MacCutchan 4-Apr-22 5:36am    
You need to do what Google is telling you in the error message.

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