介绍
这是我第一次尝试写了一篇文章。本文将着眼于如何使用的HttpModule记录电子邮件读取数据库中的通知。背景
有相当多的方式获得的电子邮件阅读通知。NET的应用程序。我们将着眼于使用HttpModule和图像标记的电子邮件读取记录的日期时间等选项之一。下面是一步一步如何创建这个选项为您的应用程序的指南。使用代码首先,我们将创建一个数据库称为SystemEmailsAudit的表。
//
CREATE TABLE [dbo].[SystemEmailsAudit]( [RecordId] [int] IDENTITY(1,1) NOT NULL,
[UserId] [varchar](20) NULL,
[EmailFor] [varchar](500) NULL,
[DateSent] [datetime] NULL,
[UniqueKey] [varchar](50) NULL,
[DateRead] [datetime] NULL ) ON [PRIMARY]
此表将继续跟踪所有的电子邮件,thenbsp;系统发送给用户,我们将登记表中的记录时,系统发出的电子邮件。为了这个目的,我创建了两个存储过程,注册电子邮件审计系统发出的电子邮件,并更新电子邮件阅读日期,当用户读取电子邮件。在数据库中创建存储过程,登记和更新SystemEmailsAudit表的。{C}创建一个通用类,将调用注册的存储过程和发送电子邮件。我有一个数据库实用工具类,重载方法处理的ExecuteNonQuery()方法。
在Visual Studio中创建一个类库项目,叫做ImageTracker和实现IHttpModule接口。你必须添加引用System.Web命名空间。本的HttpModule会拦截所有的请求的ASP.NET管道和匹配,看看它是通过比较请求的URL从电子邮件。在电子邮件正文中,我们将添加一个URL匹配模式,并增加了独特的键值来跟踪它回确切的用户系统发送电子邮件。在下面的例子中,我们添加一个事件处理程序,看起来所有的传入请求,并检查URL匹配quot;〜/图像/公升; keyvaluegt; aspxquot;模式。请注意,您不要求LT; keyvaluegt的,aspx文件在您的项目中的任何地方,因为它是由不同的图像(footerFile)取代时,我们匹配模式。{体C3}在您的应用程序,添加一个这个ImageTracker项目的参考。在您的应用程序项目,去参考,并右键单击它并选择"添加引用",选择"浏览"标签,并添加引用到ImageTracker DLL文件。在您的应用程序的web.config文件中,添加的HttpModule:{的C4}创建一个电子邮件的身体有一个形象的标签相匹配的模式,我们要跟踪的模板。{C5的} public class Common
{
public string RegisterSystemEmailAudit(string userId,
string emailFor, DateTime sentDate)
{
SqlParameter uniqueKeyParam = new SqlParameter
("@UniqueKey", SqlDbType.VarChar);
uniqueKeyParam.Direction = ParameterDirection.Output;
uniqueKeyParam.Size = 255;
public string conString = ConfigurationManager.ConnectionStrings
["MyConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
DatabaseUtility.Connection = con;
SqlParameter[] userParams = new SqlParameter[] {
new SqlParameter("@UserId",userId),
new SqlParameter("@EmailFor",emailFor),
new SqlParameter("@DateSent",sentDate),
uniqueKeyParam
};
con.Open();
SqlCommand cmd = DatabaseUtility.ExecuteNonQuery(con,
"register_system_email_audits",
CommandType.StoredProcedure,true, userParams);
con.Close();
if (cmd !=null)
{
return cmd.Parameters["@UniqueKey"].Value.ToString(); ;
}
else
{
return "N/A";
}
}
public static void SendMailMessage
(string to, string subject, string message)
{
try
{
MailMessage mailMsg = new MailMessage();
result = to.Split(new char[]{','},
StringSplitOptions.RemoveEmptyEntries);
for (int count = 0; count < result.Length; count++)
{
mailMsg.To.Add(new MailAddress(result[count]));
}
mailMsg.Bcc.Add(ConfigurationManager.AppSettings
["BCCEmail"].ToString().Trim());
mailMsg.From = new MailAddress(ConfigurationManager.AppSettings
["FromEmail"]);
mailMsg.Subject = subject;
mailMsg.Body = message;
mailMsg.IsBodyHtml=true;
//mailMsg.Headers.Add("Disposition-Notification-To",
"receiptto@email.com");
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
//
smtpClient.Send(mailMsg);
}
catch (Exception exc)
{
// Deal with exception here
}
}
}
在上面的例子中,注意:注意通过注册只读日期更新的SystemEmailsAudit表中的记录:在您的生产系统,你必须更换本地主机:49207你喜欢www.mysite.com的实际域名。如何使用上述电子邮件模板。{5233}历史2011年10月,13日:初始版本