user2024024
user2024024

Reputation: 209

how to execute a method one time

I have an email method that I am calling OnPageLoad in C# 4.0.When the page loads it sends the E-Mail to the user. Problem is that when the user refreshes the page, each time it generates the E-Mail to user which is not acceptable.Is there any way that my method executes only the first time when the page opens.I am calling the method like this

if (!IsPostBack)
{
  Sendemail(emailid, "[email protected]", "SML Dashboards Access", "<b><font color=red>Dashboard Access Alert!</font></b> <br></br>Note: If the access is by yourself, then please ignore this email.<br><br>SML Dashboards accessed using your credentials<br><b>" + emailid + "<br><b>Location / IP Address :</b>" + GetUserIP());
}

This way, the user always receives email when he refreshes the page. How can I avoid it?

Upvotes: 0

Views: 891

Answers (3)

Vladimir Bozic
Vladimir Bozic

Reputation: 444

You can optional store a cookie and check if cookie exists before sending an email. This is the simplest way, best way would be to hold the variable in user profile table in the database if you have one. That is the safest way cause cookies and session are not limitless.

Upvotes: 1

prasadrahul
prasadrahul

Reputation: 105

As mentioned by fellow stack-starTrekkers, ways to do would be

Store information on client system -- obvious perils :(

Maintain session variable -- reset events will hamper its usability - this situation can be handled by persisting to local disk or since the service could be handled by multiple server machines, to a globally viewable (for service servers) persistence layer which could be a stream, db, location up in cloud.

Other solution could be to move this logic from page to mailing service - which is more work but much cleaner approach.

Upvotes: 0

Anri
Anri

Reputation: 6265

if(Session[emailid]==null)
{
  // SEND

  Session[emailid]==true;
} 

Remember that session may reset under different circumstances like timeout or IIS restart, and email will be sent again with next session start. If you want this email to be sent just once a lifetime - you will have to persist sent flag in a database.

Upvotes: 0

Related Questions