using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SK.MemberBLL;

namespace SK.MemberWeb.admin
{
	/// <summary>
	/// Mail_ServerEdit 的摘要说明。
	/// </summary>
	public class Mail_ServerEdit : BasePage
	{
		protected System.Web.UI.WebControls.CheckBox chkAuthLogin;
		protected System.Web.UI.WebControls.TextBox SmtpServer;
		protected System.Web.UI.WebControls.TextBox SmtpEmail;
		protected System.Web.UI.WebControls.TextBox SmtpAccount;
		protected System.Web.UI.WebControls.TextBox SmtpPassword;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.Button btnSendTest;
		protected System.Web.UI.WebControls.TextBox EmailTest;
		MainSystem main = new MainSystem();
		RightMemberSystem rightmember = new RightMemberSystem();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(!IsPostBack)
			{
				if(!rightmember.Allow(0,"Mail",this.CurrentMemberID))
					Response.Redirect("noright.aspx");

				BindData();
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.btnSendTest.Click += new System.EventHandler(this.btnSendTest_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		private void BindData()
		{
			SmtpServer.Text = Main.GetValue("SmtpServer").ToString();
			SmtpEmail.Text = Main.GetValue("SmtpEmail").ToString();
			SmtpAccount.Text = Main.GetValue("SmtpAccount").ToString();
			SmtpPassword.Text = Main.GetValue("SmtpPassword").ToString();
			chkAuthLogin.Checked = Convert.ToBoolean(Main.GetValue("SMTPServerAuthLogin"));
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			main.Update("SmtpServer",SmtpServer.Text);
			main.Update("SmtpEmail",SmtpEmail.Text);
			main.Update("SmtpAccount",SmtpAccount.Text);
			main.Update("SmtpPassword",SmtpPassword.Text);
			main.Update("SMTPServerAuthLogin",chkAuthLogin.Checked);
			this.AlertAndRedir("邮件服务配置保存成功","Mail_ServerEdit.aspx");
		}

		private void btnSendTest_Click(object sender, System.EventArgs e)
		{
			try
			{
				Utils.SendMail(SmtpEmail.Text,SmtpEmail.Text,"会员系统网站测试信","如果您收到了这封信，表明您的配置是正确的!");
				this.AlertAndRedir("邮件发送成功，请检查邮箱"+SmtpEmail.Text+"中是否已经收到测试信!","Mail_ServerEdit.aspx");
			}
			catch(Exception ex)
			{
				this.DisplayAlert("邮件发送失败，请检查上述参数!"+ex.Message);
				//Response.Write(ex.Message);
			}
		}
	}
}
