DataSet ds = new DataSet ();
Ds. READXML (AppDomain.CurrentDomain.BaseDirectory + "Xml\\mail.xml");
if (ds. Tables.count > 0)
{
DataTable dt = ds. Tables[0];
if (dt. Rows.Count > 0)
{
BODY = dt. rows[0]["Content"]. ToString ();
}
}
BODY = body. Replace ("$Name $", CustName). Replace ("$DateTime $", DateTime.Now.ToString ("yyyy mm month DD Day")). ToString ();
Attachment. Name = "attachment name";
String subject = "message subject";
String to= "Mail Recipient";
Send (to, subject, body, attachment); Send mail
Here's the Send method.
public static bool Send (string to, string subject, String body, Attachment Attachment)
{
to = to. Replace (";", ";");
String from = "163 mailbox";
String userName = "163 login name";
String pwd = "163 password";
String host = "Smtp.163.com"; //163 mailbox host
int port =;
MailAddress MailSender = new MailAddress (from);
String[] tos = to. Split (';');
MailMessage mailMsg = new MailMessage (from, Tos[0], subject, body);
if (TOS. Length > 0)
{
for (int i = 1; i<tos. length;i++)
{
MailMsg.CC.Add (tos[i]);
}
}
mailmsg.isbodyhtml = true;
Mailmsg.sender = MailSender;
mailmsg.subjectencoding = Encoding.UTF8;
mailmsg.bodyencoding = Encoding.UTF8;
mailmsg.headersencoding = Encoding.UTF8;
if (attachment! = NULL)
{
MAILMSG.ATTACHMENTS.ADD (attachment);
}
New Macrowing.Common.SystemEx.SysException ("2");
SmtpClient client = new SmtpClient (host, Port);
Client. Credentials = new NetworkCredential (UserName, PWD);
Try
{
Client. Send (MAILMSG);
New Macrowing.Common.SystemEx.SysException ("3");
return true;
}
catch (Exception e)
{
New Macrowing.Common.SystemEx.SysException ("4");
Log.writeemailsendexcptionlog (E.message);
}
New Macrowing.Common.SystemEx.SysException ("5");
return false;
}
The Mail.xml configuration in XML, the entire HTML page can be copied but written in <! [cdata[content]]>, the following example:
<?xml version= "1.0" encoding= "Utf-8"?>
<MailTemplate>
<mail>
<!--do not change--
<name>Delay</name>
<!--configurable-
<title></title>
<!--configurable-
<content>
<! [cdata[
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>untitled document</title>
<style>
body,table,td,div,p,form{
padding:0px;
border-width:0px;
font-family: "Tahoma", "Arial", "Verdana", "Helvetica", "Sans-serif";
Color: #414141;
font-size:14px;
}
. colorfff{
Color: #FFF;
}
. colorff6602{
Color: #ff6602;
}
</style>
<body>
<div class= "Mailform" >
<div class= "Mailtitle colorfff fontbold" > title </div>
<p> respected <span class= "FontBold fontsize16" > $Name $</span> </p>
<p> content </p>
<p> content </p>
<p> ... </p>
<p>==================================</p>
<p>==================================</p>
<br/>
<p class= "lineheight" >best regards<br/>
XXX Team <br/>
$DateTime $
</p>
</div>
</body>
]]>
</content>
<!--configurable-
<to> $Manager $, $Actor $</to>
<!--configurable-
<cc> $Admin $</cc>
</mail>
</MailTemplate>