C# 使用asp.net进行电子邮件跟踪
我想追踪读过我邮件的用户。我正在这样做,但它不起作用 我正在outlook中向自己发送邮件。 这是我发送邮件的代码C# 使用asp.net进行电子邮件跟踪,c#,C#,我想追踪读过我邮件的用户。我正在这样做,但它不起作用 我正在outlook中向自己发送邮件。 这是我发送邮件的代码 try { string emailTemplateBody = "Hy this is test mail"; emailTemplateBody += "<tr><img src=''http://localhost:52583/HttpModule_using_beacon_images/images/&
try
{
string emailTemplateBody = "Hy this is test mail";
emailTemplateBody += "<tr><img src=''http://localhost:52583/HttpModule_using_beacon_images/images/<keyvalue>.aspx'' style=''opacity:0.0; filter:alpha(opacity=0);'' /></tr>";
string templateName = txtTemplateName.Text;
string toEmail = mymailaddress
//// Get unique Key after registring mail to be sent
string key = bl_email_calls.RegisterSystemEmailAudit("1", templateName, DateTime.Now);
emailTemplateBody = emailTemplateBody.Replace("<keyvalue>", key);
//// sending e-mail
bl_email_calls.SendMailMessage(toEmail, templateName, emailTemplateBody, key);
using (var cn = new SqlConnection(ConfigurationManager.ConnectionStrings["webConnectionString"].ToString()))
{
//code to insert record in database; }
Response.Write("Mail sent");
// return false;
}
catch (Exception ex)
{
throw;
}
试试看
{
字符串emailTemplateBody=“这是测试邮件”;
emailTemplateBody+=“.aspx”样式=''不透明度:0.0;过滤器:alpha(不透明度=0);''/>”;
字符串templateName=txtTemplateName.Text;
字符串toEmail=mymailaddress
////注册要发送的邮件后获取唯一密钥
string key=bl_email_calls.RegisterSystemEmailAudit(“1”,templateName,DateTime.Now);
emailTemplateBody=emailTemplateBody.Replace(“,键);
////发送电子邮件
bl_email_calls.SendMailMessage(toEmail、templateName、emailTemplateBody、key);
使用(var cn=new-SqlConnection(ConfigurationManager.ConnectionStrings[“webConnectionString”].ToString())
{
//在数据库中插入记录的代码;}
回复。写信(“已发送邮件”);
//返回false;
}
捕获(例外情况除外)
{
投掷;
}
这是我使用的HTTP模块http://www.aspnetemail.com/samples/emailtracker/default.aspx[^]
public class HttpModuleClass : IHttpModule
{
//public event EventHandler BeginRequest;
public void Dispose()
{
}
/// <summary>
/// public varibles
/// </summary>
string footerFile = "~/images/footer.png";
//string footerFile = "~/images/ajax-loader.gif";
Email_Calls bl_email_calls = new Email_Calls();
/// <summary>
/// Init methoed
/// </summary>
/// <param name="context"></param>
public void Init(HttpApplication context)
{
context.BeginRequest += new System.EventHandler(GetImage_BeginRequest);
}
/// <summary>
/// handles requests made to server and call update email read time
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
public void GetImage_BeginRequest(object sender, System.EventArgs args)
{
//cast the sender to a HttpApplication object
System.Web.HttpApplication application = (System.Web.HttpApplication)sender;
string url = application.Request.Path; //get the url path
//string pattern = @"/HttpModule/images/(?<key>.*)\.aspx";
//string pattern = @"/HttpModule_using_beacon_images/images/(?<key>.*)\.aspx";
string pattern = @"/HttpModule_using_beacon_images/images/(?<key>.*)\.aspx";
//string pattern = @"~/images/(?<key>.*)\.aspx";
//create the regex to match for beacon images
Regex r = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (r.IsMatch(url))
{
MatchCollection mc = r.Matches(url);
if ((mc != null) && (mc.Count > 0))
{
string key = (mc[0].Groups["key"].Value);
bl_email_calls.UpdateSystemEmailAuditReadDate(key);
}
//now send the REAL image to the client
//application.Response.ContentType = "image/gif";
application.Response.ContentType = "image/png";
application.Response.WriteFile(application.Request.MapPath(footerFile));
//end the response
application.Response.End();
}
}
}
公共类HttpModuleClass:IHttpModule
{
//公共事件处理程序BeginRequest;
公共空间处置()
{
}
///
///公共变量
///
字符串footerFile=“~/images/footer.png”;
//string footerFile=“~/images/ajax loader.gif”;
Email_Calls bl_Email_Calls=新的Email_Calls();
///
///初始方法
///
///
公共void Init(HttpApplication上下文)
{
context.BeginRequest+=new System.EventHandler(GetImage_BeginRequest);
}
///
///处理向服务器发出的请求,并在读取时调用更新电子邮件
///
///
///
public void GetImage_BeginRequest(对象发送方,System.EventArgs args)
{
//将发送方强制转换为HttpApplication对象
System.Web.HttpApplication application=(System.Web.HttpApplication)发送方;
string url=application.Request.Path;//获取url路径
//字符串模式=@“/HttpModule/images/(?.*)\.aspx”;
//字符串模式=@“/HttpModule\u使用信标图像/images/(?*)\.aspx”;
字符串模式=@“/HttpModule\u使用信标图像/images/(?*)\.aspx”;
//字符串模式=@“~/images/(?.*)\.aspx”;
//创建正则表达式以匹配信标图像
正则表达式r=新正则表达式(模式,RegexOptions.Compiled | RegexOptions.IgnoreCase);
if(r.IsMatch(url))
{
MatchCollection mc=r.Matches(url);
如果((mc!=null)&&(mc.Count>0))
{
字符串键=(mc[0]。组[“键”]。值);
bl_email_calls.UpdateSystemEmailAuditReadDate(键);
}
//现在将真实图像发送到客户端
//application.Response.ContentType=“image/gif”;
application.Response.ContentType=“image/png”;
application.Response.WriteFile(application.Request.MapPath(footerFile));
//结束回应
application.Response.End();
}
}
}
要请求已读回执,我们需要添加一个名为“Disposition-Notification-To”的自定义标题。
在本例中,已读回执将返回到'someaddress@mydomain.com'
需要注意的是,读取回执只会由那些
a) 支持他们
和
b) 启用它们
//将处置通知添加到“”以读取收据
mail.Headers.Add("Disposition-Notification-To", "<mail@yahoo.com>");
mail.Headers.Add(“处置通知到“,”);
我明白了。我犯了一些小错误
实际上,我正在将电子邮件正文保存到数据库中,因此我不得不使用
'
而不是'
,这就造成了所有的麻烦。当删除“”时,它工作正常。Outlook将为不受信任的发件人或用户未启用显示图像选项的发件人阻止邮件中的图像,主要是为了防止此类跟踪。例如,请参见为什么省略?ie…src=''http:…