C#中发送邮件时模板处理

在c#的项目中,常常需要添加邮件发送的功能,邮件发送时的模板往往不是一成不变的,是需要根据case的不同来确定不同内容的邮件,发送给不同的对象,这里分享一种邮件模板处理的方式,仅供参考(邮件发送的函数参考http://www.cnblogs.com/dannyli/archive/2011/08/23/2150229.html

1.邮件模板(红色部分是需要根据case来确定的)

2.模板处理类

/*=============================================================
* Template 处理
* Author : Danny,Li
* E-mail : xing.dong.li@163.com
* Edition: V-101014
*=============================================================
*/
using System;
using System.Data;
using System.Configuration;
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.Collections.Specialized;
using System.Collections;
using System.IO;

namespace IRCommon
{
public class TemplateHelper
{
/// <summary>
/// 私有构造方法,不允许创建实例
/// </summary>
private TemplateHelper()
{
// TODO: Add constructor logic here
}

/// <summary>
/// Template File Helper
/// </summary>
/// <param name="templatePath">Templet Path</param>
/// <param name="values">NameValueCollection</param>
/// <returns>string</returns>
public static string BulidByFile(string templatePath, NameValueCollection values)
{
return BulidByFile(templatePath, values, "[$", "]");
}

/// <summary>
///
/// </summary>
/// <param name="template"></param>
/// <param name="values">NameValueCollection obj</param>
/// <param name="prefix"></param>
/// <param name="postfix"></param>
/// <returns></returns>
public static string Build(string template, NameValueCollection values, string prefix, string postfix)
{
if (values != null)
{
foreach (DictionaryEntry entry in values)
{
template
= template.Replace(string.Format("{0}{1}{2}", prefix, entry.Key, postfix), entry.Value.ToString());
}
}
return template;
}

/// <summary>
///
/// </summary>
/// <param name="templatePath"></param>
/// <param name="values"></param>
/// <param name="prefix"></param>
/// <param name="postfix"></param>
/// <returns></returns>
public static string BulidByFile(string templatePath, NameValueCollection values, string prefix, string postfix)
{
StreamReader reader
= null;
string template = string.Empty;
try
{
reader
= new StreamReader(templatePath);
template
= reader.ReadToEnd();
reader.Close();
if (values != null)
{
foreach (string key in values.AllKeys)
{
template
= template.Replace(string.Format("{0}{1}{2}", prefix, key, postfix), values[key]);
}
}
}
catch
{

}
finally
{
if (reader != null)
reader.Close();
}
return template;
}
}
}

3.c#的邮件发送函数

//发送邮件通知
private void SendMail(string email, string name, string sdate, string edate, string course, string point)
{
string mailFrom = MailHelper.MailServerFrom;
string mailTo = string.Empty;
string mailSubject = string.Empty;
string mailBody = string.Empty;
string mailAttch = string.Empty;
string mailCode = string.Empty;
string mailPriority = string.Empty;
string mailCC = string.Empty;
string resultMessage = "";
if (!string.IsNullOrEmpty(email))
{
mailTo
= email;
mailSubject
= "Change By Innovation 加分通知";
try
{
string templetpath = Server.MapPath("../mailtemplate/irupoint.txt");
NameValueCollection myCol
= new NameValueCollection();
myCol.Add(
"ename", name);
myCol.Add(
"sdate", sdate);
myCol.Add(
"edate", edate);
myCol.Add(
"course", course);
myCol.Add(
"point", point);
myCol.Add(
"link", System.Configuration.ConfigurationManager.AppSettings["rootUrl"]);
mailBody
= TemplateHelper.BulidByFile(templetpath, myCol);
MailHelper.SendNetMail(mailFrom, mailTo, mailSubject, mailBody, mailAttch, mailCode, mailPriority, mailCC,
out resultMessage);
}
catch (Exception ex)
{
}
}
}

4.目录中的mailtemplate/irupoint.txt

<html>
<head>
<title>Change By Innovation 加分通知</title>
<style type="text/css">
body
{ font-family:Arial,"宋体"; font-size: 12px; font-weight: normal; color: #666666; }
</style>
</head>
<body bgcolor="#F6FAFB">
<p>[$ename] 您好,</p>
<p>
&nbsp;&nbsp;&nbsp;&nbsp;
您在 [$sdate] 至 [$edate] 参加IRU培训课程《[$course]》,系统为您添加 [$point] 分。
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;
感谢您对 Change By Innovation 的支持,请
<a href='[$link]'>点击此处</a>登陆系统,查看您的总积分及排名信息。
</p>
<br><br>
<p>&nbsp;&nbsp;&nbsp;&nbsp;系统邮件,请勿回复!</p>
</body>
</html>

5.邮件发送的函数MailHelper.SendNetMail(...)

参考http://www.cnblogs.com/dannyli/archive/2011/08/23/2150229.html

需要在配置文件中配置SMTP服务器等信息

转载于:https://www.cnblogs.com/dannyli/archive/2011/08/23/2150260.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值