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月dd日")).ToString();
attachment.Name = “附件名字”;
string subject = "邮件主题";
string to= "邮件接收人";
Send(to, subject, body, attachment); //邮件发送
// 一下是Send 方法
public static bool Send(string to, string subject, string body, Attachment attachment)
{
to = to.Replace(";", ";");
string from = "163邮箱";
string userName = "163登录名";
string pwd = "163密码";
string host = "smtp.163.com"; //163邮箱 host
int port = 25;
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;
}
/// XML 中的mail.xml 配置 、 可复制整个html 网页但是要写在 <![CDATA[ 内容 ]]> 之间,如下例子:
<?xml version="1.0" encoding="utf-8" ?>
<MailTemplate>
<mail>
<!-- 请勿修改 -->
<name>Delay</name>
<!-- 可配置 -->
<title></title>
<!-- 可配置 -->
<content>
<![CDATA[
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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>
</head>
<body>
<div class="mailForm">
<div class="mailTitle colorFFF fontbold">标题</div>
<p>尊敬的 <span class="fontbold fontsize16">$Name$</span>,您好!</p>
<p>内容</p>
<p>内容</p>
<p>。。。。。。</p>
<p>==================================</p>
<p>==================================</p>
<br />
<p class="lineheight">Best Regards<br />
XXX团队<br />
$DateTime$
</p>
</div>
</body>
</html>
]]>
</content>
<!-- 可配置 -->
<to>$Manager$,$Actor$</to>
<!-- 可配置 -->
<cc>$Admin$</cc>
</mail>
</MailTemplate>