0

I have 2 aspx.cs pages. First page is for login, after login i call to method which send and email as alert that a user loged in. After log in you move to second page. I want to create another button in the second page, when you press it, it will send email again (the same sending method from page 1). Can i in page 2, call to the "send" method from page 1 or i must write the entire method in each page?

This is page 1 code behind:

public partial class UserLogin : System.Web.UI.Page
{
private string _employee;
private string _userName;
private string _loginDatetime;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void LoginBtn_Click(object sender, EventArgs e)
{
    _employee = employeeBox.Text;
    _userName = userBox.Text;
    _loginDatetime = DateTime.Now.ToString();

    SendMail("your_email@gmail.com", "your_email@gmail.com", "your_email@gmail.com", "Login Alert",
        "Hello " + _employee + ",<Br>User \"" + _userName + "\" just log to the website on the following date: " + DateTime.Now);

    Response.Redirect("http://localhost:21361/UserDeposit.aspx?_employee=" + employeeBox.Text + "&_userName=" + userBox.Text + "&_loginDatetime=" + _loginDatetime);
}

protected string SendMail(string toList, string from, string ccList, string subject, string body)
{

    MailMessage message = new MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg;
    try
    {
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        if (!string.IsNullOrEmpty(ccList))
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Host = "smtp.gmail.com";   
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new NetworkCredential("your_email@gmail.com", "password");

        smtpClient.Send(message);
        msg = "Successful";
        Response.Write("<script>alert('" + msg + "')</script>");
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        Response.Write("<script>alert('" + msg + "')</script>");
    }
    return msg;
}
}

This is page 2 code behind:

public partial class UserDeposit : System.Web.UI.Page
{
string _employeeName;
string _userName;
string _loginTime;
DateTime time;

protected void Page_Load(object sender, EventArgs e)
{
    _employeeName = Request.QueryString["_employee"];
    EmployeeName.Text = _employeeName;

    _userName = Request.QueryString["_userName"];
    UserName.Text = _userName;

    _loginTime = Request.QueryString["_loginDatetime"];
    time = DateTime.Parse(_loginTime);
}



protected void Button1_Click(object sender, EventArgs e)
{
    //call here the "send mail" method??
}
}
nirh1989
  • 199
  • 2
  • 6
  • 15

3 Answers3

2

What about 3: You do not have the send method in any page but put it in a separate class.

That is proper programming practice.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • in regular C# file, i can create an instance for the class where the method is, and than the method write only once, and i can call her whenever i want. but i do not know how to do this with aspx.cs – nirh1989 Apr 06 '16 at 10:00
  • 1
    You do not in aspx.cs. You do it by adding a class file into the project. Sorry, but did you ever learn how to use asp.NET? What you think the App_Code folder is for? http://stackoverflow.com/questions/7480437/asp-net-app-data-app-code-folders – TomTom Apr 06 '16 at 10:02
  • i am new to asp.net... The second page do not recognize the first page, it's like it's missing a reference between the pages, so i cannot call a method from 1 page to another – nirh1989 Apr 06 '16 at 10:02
0

You need to do either one of the below methods:

  1. Create a base Page class and inherit that base page class in all appropriate aspx pages. (preferred method)

  2. Create a static class and put them there.

0

You make the SendMail method static like this:

public static string SendMail(string toList, string from, string ccList, string subject, string body)

Now you can call it from anywhere, just append the class name in front, like this:

UserLogin.SendMail(toList, from, ccList, subject, body);
Poul Bak
  • 10,450
  • 5
  • 32
  • 57