XML:
<?xml version="1.0" encoding="utf-8"?>
<mail>
<smtp from="yyc_163@163.com" pwd="ixafhp" fromName="Ricoh" cc="youcai@chinasoftware.eu" needPWD="1">
<network host="smtp.163.com" port="25" />
<validfield>
{ProductType},{InvoiceNumber},{Name},{Address},{Postcode},{City},{Email},{Note},{Status},{CreateDate}
</validfield>
</smtp>
<acceptmail>
<subject>Accept Mail</subject>
<body>Dear Customer,
This is a Accept Mail.
{ProductType},
{InvoiceNumber},
{Name},
{Address},
{Postcode},
{City},
{Email},
{Note},
{Status},
{CreateDate}</body>
</acceptmail>
<rejectmail>
<subject>Reject Mail</subject>
<body>Dear Customer,
This is a reject Mail.
{ProductType},
{InvoiceNumber},
{Name},
{Address},
{Postcode},
{City},
{Email},
{Note},
{Status},
{CreateDate}</body>
</rejectmail>
</mail>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class Admin_MailTemplate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindMailTemplate();
}
}
private void BindMailTemplate()
{
string filePath = HttpContext.Current.Server.MapPath("~/Config/MailTemplate.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode node = xmlDoc.SelectSingleNode("mail");
string from = node["smtp"].Attributes["from"].Value;
string pwd = node["smtp"].Attributes["pwd"].Value;
string needPWD = node["smtp"].Attributes["needPWD"].Value;
string fromName = node["smtp"].Attributes["fromName"].Value;
string cc = node["smtp"].Attributes["cc"].Value;
string host = node["smtp"]["network"].Attributes["host"].Value;
string hostPort = node["smtp"]["network"].Attributes["port"].Value;
string validField = node["smtp"]["validfield"].InnerText;
string subjectAccept = node["acceptmail"]["subject"].InnerText;
string bodyAccept = node["acceptmail"]["body"].InnerText;
string subjectReject = node["rejectmail"]["subject"].InnerText;
string bodyReject = node["rejectmail"]["body"].InnerText;
this.txtFrom.Text = from;
this.txtPassword.Text = pwd;
this.txtPassword.Attributes.Add("value", pwd);
this.cbxNeedPWD.Checked = needPWD == "1" ? true : false;
this.txtFromName.Text = fromName;
this.txtCopyTo.Text = cc;
this.txtHost.Text = host;
this.txtPort.Text = hostPort;
this.txtAcceptSubject.Text = subjectAccept;
this.txtAcceptBody.Text = bodyAccept;
this.txtRejectSubject.Text = subjectReject;
this.txtRejectBody.Text = bodyReject;
this.txtValidFild.Text = validField;
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
string filePath = HttpContext.Current.Server.MapPath("~/Config/MailTemplate.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode node = xmlDoc.SelectSingleNode("mail");
node["smtp"].Attributes["from"].Value = this.txtFrom.Text;
node["smtp"].Attributes["fromName"].Value = this.txtFromName.Text;
node["smtp"].Attributes["cc"].Value = this.txtCopyTo.Text;
node["smtp"].Attributes["needPWD"].Value = this.cbxNeedPWD.Checked ? "1" : "0";
node["smtp"]["network"].Attributes["host"].Value = this.txtHost.Text;
node["smtp"]["network"].Attributes["port"].Value = this.txtPort.Text;
node["acceptmail"]["subject"].InnerText = this.txtAcceptSubject.Text;
node["acceptmail"]["body"].InnerText = this.txtAcceptBody.Text;
node["rejectmail"]["subject"].InnerText = this.txtRejectSubject.Text;
node["rejectmail"]["body"].InnerText = this.txtRejectBody.Text;
if (!string.IsNullOrEmpty(this.txtPassword.Text))
{
node["smtp"].Attributes["pwd"].Value = this.txtPassword.Text;
}
xmlDoc.Save(filePath);
this.Message1.ShowInfo("Save successfully!");
//Response.Write("<script>alert('geslaagd');window.location='MailTemplate.aspx';</script>");
}
catch(Exception ex)
{
this.Message1.ShowError("Fail to save!<br/>" + ex.Message.Replace("'", ""));
//Response.Write("<script>alert('niet'" + ex.Message.Replace("'", "") + ");history.back(-1)</script>");
}
}
}
HTML:
<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/BackPage.master" ValidateRequest="false" AutoEventWireup="true" CodeFile="MailTemplate.aspx.cs" Inherits="Admin_MailTemplate" %>
<%@ Register src="Navigator.ascx" tagname="Navigator" tagprefix="uc1" %>
<%@ Register src="Message.ascx" tagname="Message" tagprefix="uc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<uc1:Navigator ID="Navigator1" runat="server" />
<uc2:Message ID="Message1" runat="server" />
<table style="background-color:#BFE7F6; width:100%;" >
<tr>
<td style="width:100px">Host:</td> <td> <asp:TextBox ID="txtHost" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Port:</td> <td> <asp:TextBox ID="txtPort" runat="server" onchange="if(/\D/.test(value))value=value.replace(/\D/g,'')" ></asp:TextBox></td>
</tr>
<tr>
<td>From:</td> <td> <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>FromName:</td> <td> <asp:TextBox ID="txtFromName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Need Password:</td> <td> <asp:CheckBox ID="cbxNeedPWD" runat="server" /></td>
</tr>
<tr>
<td>Password:</td> <td> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td>CopyTo:</td> <td> <asp:TextBox ID="txtCopyTo" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Valid Fields:</td> <td> <asp:Label ID="txtValidFild" runat="server" ReadOnly="true" style="width:600px"></asp:Label></td>
</tr>
<tr>
<td colspan="2" style="background-color:#49BEEB; ">Accept Mail</td>
</tr>
<tr>
<td>Subject:</td> <td> <asp:TextBox ID="txtAcceptSubject" runat="server" style="width:600px"></asp:TextBox></td>
</tr>
<tr>
<td>Body:</td> <td> <asp:TextBox ID="txtAcceptBody" runat="server" TextMode="MultiLine" Rows="10" style="width:600px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="background-color:#49BEEB; ">Reject Mail</td>
</tr>
<tr>
<td>Subject:</td> <td> <asp:TextBox ID="txtRejectSubject" runat="server" style="width:600px"></asp:TextBox></td>
</tr>
<tr>
<td>Body:</td> <td> <asp:TextBox ID="txtRejectBody" runat="server" TextMode="MultiLine" Rows="10" style="width:600px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"><asp:Button ID="btnSave" runat="server" Text="Save"
onclick="btnSave_Click" /> </td>
</tr>
</table>
</asp:Content>