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??
}
}