using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Mail;
public partial class WTOSYSadmin_email_EMAILdo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        string type = Request.QueryString["type"].ToString().Trim();
        switch (type)
        {
            case "del":
                if (Doadmin.Exec("delete from email where lx='0' and type='0' and  id=" + Request.QueryString["id"]))
                { Response.Redirect("Emailmag.aspx"); }
                else { Response.Write("<script>alert('删除失败')</script>"); }
                break;
            case "send":
                SqlConnection Mconn = CDatabase.wtoconn();
                Mconn.Open();
                SqlCommand Msmd = new SqlCommand("select * from SystemBasicInformation", Mconn);
                SqlDataReader Msdr = Msmd.ExecuteReader();
                Msdr.Read();
                string Mform, Muid, Mpwd, Mstmp, Misopen, Mcont, MCompanyName, MWEBName, MTel, MFax, MEmail, MQQ, MAddress, MMSN, TBWW, MLOGOpic;
                Mform = Msdr.GetString(7);
                Muid = Mform;
                Mpwd = Msdr.GetString(26);
                Mstmp = Msdr.GetString(25);
                Misopen = Msdr.GetString(1).ToString().Trim();
                MCompanyName = Msdr.GetString(2);
                MWEBName = Msdr.GetString(3);
                MTel = Msdr.GetString(5);
                MFax = Msdr.GetString(6);
                MEmail = Msdr.GetString(7);
                MQQ = Msdr.GetString(8);
                MAddress = Msdr.GetString(10);
                MMSN = Msdr.GetString(13);
                TBWW = Msdr.GetString(14);
                MLOGOpic = "<a href=http://" + Request.ServerVariables["SERVER_NAME"].ToString() + "><img src=http://" + Request.ServerVariables["SERVER_NAME"].ToString() + "/images/" + Msdr.GetString(15) + " border=0></a>";
                Msdr.Close();
                SqlCommand cmds = new SqlCommand("select emailFL,content from email where lx='0' and type='0' and id="+Request.QueryString["id"], Mconn);
                SqlDataReader csdr=cmds.ExecuteReader();
                csdr.Read();
                string Mzt = csdr.GetString(0);
                Mcont=Server.HtmlDecode(csdr.GetString(1));
                csdr.Close();

                SqlCommand Vsmd = new SqlCommand("select * from MemberVIP where isok='y'", Mconn);
                SqlDataReader Vsdr = Vsmd.ExecuteReader();
                MailMessage msg;
                while (Vsdr.Read())
                {
                    msg = new MailMessage();
                    msg.From = Mform;  //发送方地址(如test@163.com)
                    msg.To = Vsdr.GetString(7); //接收方地址(如test@163.com)
                    msg.BodyFormat = MailFormat.Html;   //正文内容类型 
                    msg.BodyEncoding = System.Text.Encoding.Default;    //正文内容编码 
                    //msg.Attachments.Add(new MailAttachment("d:\\d.txt"));
                    //主题 
                    msg.Subject = Mzt;
                    //内容 
                    //编码定义
                    Mcont = ((Mcont.Replace("$UID$",Vsdr.GetString(3))).Replace("$PWD$",Vsdr.GetString(4)));
                    Mcont = Mcont.Replace("$CMY$", MCompanyName);
                    Mcont = Mcont.Replace("$WEBN$", MWEBName);
                    Mcont = Mcont.Replace("$TEL$", MTel);
                    Mcont = Mcont.Replace("$FAX$", MFax);
                    Mcont = Mcont.Replace("$EMA$", MEmail);
                    Mcont = Mcont.Replace("$QQ$", MQQ);
                    Mcont = Mcont.Replace("$ADD$", MAddress);
                    Mcont = Mcont.Replace("$MSN$", MMSN);
                    Mcont = Mcont.Replace("$TBW$", TBWW);
                    Mcont = Mcont.Replace("$LOGO$", MLOGOpic);
                    Mcont = Mcont.Replace("$ORN$", Vsdr.GetString(2));
                    msg.Body = Mcont;
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //设置为需要用户验证 
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Muid); //设置验证用户名 
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Mpwd); //设置验证密码 
                    SmtpMail.SmtpServer = Mstmp;  //邮件服务器地址(如smtp.163.com) 
                    SmtpMail.Send(msg);  //发送 
                }
                Mconn.Close();
                break;
            case "change":
                switch (Request.QueryString["doing"])
                { 
                    case "open":
                        if (Doadmin.Exec("update email set isopen='y' where id=" + Request.QueryString["id"].Trim()))
                        {
                            switch (Request.QueryString["lx"].Trim())
                            {
                                case "1":
                                    Doadmin.Exec("update SystemBasicInformation set RegEmailSet='y'");
                                    break;
                                case "2":
                                    Doadmin.Exec("update SystemBasicInformation set FindPswEmailSet='y'");
                                    break;
                                case "3":
                                    Doadmin.Exec("update SystemBasicInformation set EditVIPEmailSet='y'");
                                    break;
                                case "4":
                                    Doadmin.Exec("update SystemBasicInformation set ConsignmentEmailSet='y'");
                                    break;
                                case "5":
                                    Doadmin.Exec("update SystemBasicInformation set OrderEmailSet='y'");
                                    break;
                            }
                            Response.Write("<script>alert('操作成功');window.location.href='SYSemail.aspx'</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次操作失败');window.location.href='SYSemail.aspx'</script>");
                        }
                       

                            break;
                    case "close":
                        if (Doadmin.Exec("update email set isopen='n' where id=" + Request.QueryString["id"].Trim()))
                        {
                            switch (Request.QueryString["lx"].Trim())
                            {
                                case "1":
                                    Doadmin.Exec("update SystemBasicInformation set RegEmailSet='n'");
                                    break;
                                case "2":
                                    Doadmin.Exec("update SystemBasicInformation set FindPswEmailSet='n'");
                                    break;
                                case "3":
                                    Doadmin.Exec("update SystemBasicInformation set EditVIPEmailSet='n'");
                                    break;
                                case "4":
                                    Doadmin.Exec("update SystemBasicInformation set ConsignmentEmailSet='n'");
                                    break;
                                case "5":
                                    Doadmin.Exec("update SystemBasicInformation set OrderEmailSet='n'");
                                    break;
                            }
                            Response.Write("<script>alert('操作成功');window.location.href='SYSemail.aspx'</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次操作失败');window.location.href='SYSemail.aspx'</script>");
                        }
                        
                        break;
                }
                break;
        }
    }
}
