Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using asp.net web application. I am sending a mail to user in that passing a link. By pressing that link, making changes in my database. I am using following code:

It is working fine for sending email with link. I want that link should be inactive after 24 hours and pressed only once. If pressed twice should not work. How to do that?

using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
{
    mm.Subject = "Account Activation";
    string body = "Hello " + txtUsername.Text.Trim() + ",";
    body += "<br /><br />Please click the following link to activate your account";
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
    body += "<br /><br />Thanks";
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
}


What I have tried:

using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
{
    mm.Subject = "Account Activation";
    string body = "Hello " + txtUsername.Text.Trim() + ",";
    body += "<br /><br />Please click the following link to activate your account";
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
    body += "<br /><br />Thanks";
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
}
Posted
Updated 28-Jan-18 23:35pm
Comments
F-ES Sitecore 29-Jan-18 5:34am    
Update your database to have a "Email_Sent" field for the user and when you send the email update the user's Email_Sent date to show this. Before you attempt to send the email check the value of Email_Sent to see if it has been less than 24 hours since the email was sent and if so do nothing.

1 solution

Store the activation code in a DB, along with the expiry time, and the details needed to activate the account.
When they follow the link, pick up the activation code from the query string and look it up in your DB.
1) If it isn't there, it's been used.
2) If it is there, check the expiry date.
2.1) If it's still valid, activate the account and delete the activation table row.
2.2) If it's invalid, delete the row, delete the account, and show a "expired" message on the page.
 
Share this answer
 
Comments
Laxmidhar tatwa technologies 31-Jan-18 5:45am    
In db stored the date with a flag with false
When click check the date and made ur flag to true

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