一个具有动态模板的 SMTP 电子邮件客户端





0/5 (0投票)
一个简单的 SMTP 邮件客户端,
引言
本文创建一个可重用的电子邮件客户端,它具有基于 XML 模板的电子邮件服务。电子邮件正文中的特定文本可以动态修改。
本文的主要组成部分是
- 一个简单的 SMTP 邮件服务器
- 基于 XML 的模板,用于动态选择电子邮件正文和标题图标(可选)。
- 动态替换电子邮件正文中的特定条目。此功能由 XML 驱动。
- 使用 XSD 和设计器类进行 XML 序列化。
背景
对于应用程序,可能需要在应用程序执行的特定阶段向用户发送多封邮件。我们还需要动态格式化电子邮件,例如收件人。因此,我们尝试创建一个由 XML 驱动的电子邮件模板引擎。我们可以从任何来源获取动态数据。它可以是数据库或另一个 XML 源。
我们可以将所需的信息(如 To/From 电子邮件地址)设置为类,并提及一个模板。其余的将由引擎处理。
使用代码
需要添加的命名空间是
using System.Net.Mail;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.IO;
using System.Xml.Serialization;
“MailInfo
”类用于保存发送电子邮件所需的基本数据。
class MailInfo
{
public string ToAddress { get; set; }
public string FromAddress { get; set; }
public string Subject { get; set; }
public MailInfo(string toAdd, string fromAdd, string subject)
{
ToAddress = toAdd;
FromAddress = fromAdd;
Subject = subject;
}
public MailInfo()
{
}
}
MailType
枚举是电子邮件模板引擎的驱动因素。这基本上是所有可能电子邮件类型的词汇表。
enum MailType
{
Unknown,
Success,
Failure
}
SMTPEmailClient
类是负责创建和发送电子邮件的父类。
它包含一个 SmtpClient
对象作为成员变量。这用于发送消息。
private SmtpClient smtpServer = null;
方法
SetSMTPServerInfo
此方法用于初始化 SMTP 服务器。
public void SetSMTPServerInfo(string ipAddresss,int portnumber =25,bool isSecured =false)
{
try
{
smtpServer = new SmtpClient(ipAddresss);
smtpServer.Port = portnumber;
smtpServer.EnableSsl = isSecured;//by default this is false
}
catch (Exception)
{
throw;
}
}
GetReplacements
此方法返回一个 ListDictionary
,该字典与 MailMessage
对象一起使用以动态更新电子邮件内容。
private ListDictionary GetReplacements(List<EmailTemplatesEmailInput> list)
{
ListDictionary replacements = null;
try
{
replacements = new ListDictionary();
foreach (EmailTemplatesEmailInput item in list)
{
replacements.Add("<%" + item.type + "%>", GetDataFromType(item.type));
}
}
catch (Exception)
{
throw;
}
return replacements;
}
GetDataFromType
此方法的工作是根据 XML 标记从任何数据源检索动态数据。到目前为止,示例代码返回硬编码数据:
private string GetDataFromType(string type)
最后,主要的入口点方法是 SendEmail
。
SendEmail
此方法将 MailInfo
和 MailType
作为参数。
它按顺序执行以下操作
- 使用序列化从 XML 文件中获取与邮件相关的模板信息
- 根据
MailType
检索模板。 - 接下来,它创建一个
MailMessage
和MailDefinition
并根据输入更新结构。 - 使用
MailDefinition
中存在的CreateMailMessage
方法,我们格式化电子邮件正文 。 - 最后,我们为文本和图标创建 AlternateViews,并将它们添加到
MailMessage
。 - 使用
smtpServer.Send(msg)
发送电子邮件。
public bool SendEmail(MailInfo info, MailType type)
模板文件。
到目前为止,模板文件是根据以下架构的 XML
<?xml version="1.0" encoding="utf-8" ?>
<EmailTemplates>
<Email type ="Success">
<!-- the filaname gives the name of the file-->
<FileName>Sample1.txt</FileName>
<!--the HeaderIcon is an optional tag-->
<HeaderIcon>CompletedGreenMark.png</HeaderIcon>
<!--Content needs to be removed-->
<content>
</content>
<!--The inputList contains the tags which needs to be replaced in the body-->
<InputsList>
<Input type= "Name"/>
<Input type= "Source"/>
</InputsList>
</Email>
<Email type ="Failure">
<FileName>Sample2.txt</FileName>
<content>
</content>
<InputsList>
<Input type= "NickName"/>
<Input type= "Test"/>
</InputsList>
</Email>
</EmailTemplates>
EmailTemplates
类是使用 XSDToCode 自动生成的。
到目前为止,所有模板文件都需要存在于当前目录中
//header logo file
CompletedGreenMark.png
//Temple files with html tag
Sample1.txt
Sample2.txt
//Templates file
Template.xml
//Schema file for deserialization
Template.xsd
关注点
嗯,一直想学习 SMTP 在 dot net 中是如何工作的,而且我想让它尽可能通用。 仍然有很多工作要做。 希望这能帮助其他人开发出电子邮件客户端。
请求:
请评价这篇文章。 帮助我改进。
历史
- 18/06/12:首次创建。