-2

I am getting (Compiler Error) "Error CS0120 An object reference is required for the non-static field, method, or property ". My code is mentioned below. Can please anyone assist me how to fix this issue.

 private static Task<string> SendPasswordResetVerificationCode(string email)
    {
        string randomVerificationCode = new Random().Next(1000, 9999).ToString(CultureInfo.CurrentCulture);

        ////send verification code to given email
        if (email != null)
        {
            SendEmail(email, randomVerificationCode);
            return Task.FromResult("VerificationCode Sent");
        }

        return Task.FromResult("VerificationCode Sent");
    }

    /// <summary>
    /// Sends an email for verification code
    /// </summary>
    /// <param name="email">email address that will receive verification code</param>
    /// <param name="verificationCode">verification code</param>
    private void SendEmail(string email, string verificationCode)
    {
        Task.Run(() =>
        {
            try
            {
                    MailMessage mail = new MailMessage();
                    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
                    mail.From = new MailAddress("v@gmail.com");
                    mail.To.Add(email);
                    mail.IsBodyHtml = true;
                    mail.Subject = "Verification Code";
                     mail.Body = verificationCode;
                    smtpServer.Port = 587;

                    smtpServer.UseDefaultCredentials = false;
                    smtpServer.EnableSsl = true;
                    smtpServer.Send(mail);
                    mail.Dispose();
            }
            catch (Exception ex)
            {
                Utilities.Logging.LogManager.Write(ex);
            }
        });
    }
DaniDev
  • 2,471
  • 2
  • 22
  • 29
Raj
  • 183
  • 3
  • 17
  • You need to post more info... Which line is giving you the error? – dana Mar 16 '17 at 04:03
  • does your method have to be static? Try removing the static access modifier. – DaniDev Mar 16 '17 at 04:08
  • Where are you encountering the exception? Please inspect the exception you're seeing and provide additional information. – STLDev Mar 16 '17 at 04:08
  • This is a compiler error, correct? BTW, based on your conditional logic you should return Task.FromResult("VerificationCode Sent"); just once outside of your if statement – DaniDev Mar 16 '17 at 04:15

1 Answers1

1

Take a look into the two methods that you already having, ie., SendPasswordResetVerificationCode and SendEmail, among them SendEmail is instance method and the first one is a static method. Now take a look into the error message. it's clear enough, as it says object reference is required for the nonstatic field, method, or property when you call SendEmail from the static method.

So the fix is quick and simple change SendEmail as a static method as well. So the definition would be like the following:

private static void SendEmail(string email, string verificationCode)
{    
    // code here
}

Note : if you don't have any specific reason for SendPasswordResetVerificationCode to be static means you can remove static from the signature as well without changing the signature of the SendEmail but the actual call should be with a reference object

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88