C# 跟踪发送的邮件是否已读取

C# 跟踪发送的邮件是否已读取,c#,asp.net,email,tracking,C#,Asp.net,Email,Tracking,我搜索了很多文章,找到了一些代码,但对我来说不起作用 My index.aspx.cs页面代码为: protected void Page_Load(object sender, EventArgs e) { if (checkIfRequested(this.Context.Request)) { //receiver had already requested the image, hence send back a not modi

我搜索了很多文章,找到了一些代码,但对我来说不起作用

My index.aspx.cs页面代码为:

 protected void Page_Load(object sender, EventArgs e)
 {
    if (checkIfRequested(this.Context.Request))
    {           
        //receiver had already requested the image, hence send back a not modified result
        Response.StatusCode = 304;
        Response.SuppressContent = true;
    }
    else
    {
        int emailId = 0;
        if (!string.IsNullOrEmpty(Request.QueryString["emailId"]) && int.TryParse(Request.QueryString["emailId"], out emailId))
        {
            Session["image"] = Request.QueryString["emailId"];
            lblid.Text = Convert.ToString(Session["image"]);


            //The email with emailId has been opened, so log that in database
        }
        else
        {
            if (Request.QueryString["emailId"] != null)
            {
                lblid.Text = Convert.ToString(Request.QueryString["emailId"]);
            }
            string str = "<script>alert('empty')</script>";
            Page.RegisterStartupScript("popup", str);
        }

        //Send the single pixel gif image as response
        byte[] imgbytes = Convert.FromBase64String("R0lGODlhAQABAIAAANvf7wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
        Response.ContentType = "image/gif";
        Response.AppendHeader("Content-Length", imgbytes.Length.ToString());
        Response.Cache.SetLastModified(DateTime.Now);
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.BinaryWrite(imgbytes);
    }
}

private bool checkIfRequested(HttpRequest req)
{
    // check if-modified-since header to check if receiver has already requested the image in last 24 hours
    return req.Headers["If-Modified-Since"] == null ? false : DateTime.Parse(req.Headers["If-Modified-Since"]).AddHours(24) >= DateTime.Now;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
if(checkifrequest(this.Context.Request))
{           
//接收器已请求图像,因此返回未修改的结果
Response.StatusCode=304;
Response.SuppressContent=true;
}
其他的
{
int emailId=0;
如果(!string.IsNullOrEmpty(Request.QueryString[“emailId”])和&int.TryParse(Request.QueryString[“emailId”],out emailId))
{
会话[“image”]=Request.QueryString[“emailId”];
lblid.Text=Convert.ToString(会话[“图像]);
//已打开emailId为的电子邮件,请将其登录到数据库中
}
其他的
{
if(Request.QueryString[“emailId”!=null)
{
lblid.Text=Convert.ToString(Request.QueryString[“emailId”]);
}
string str=“警报('empty')”;
RegisterStartupScript(“弹出窗口”,str);
}
//发送单像素gif图像作为响应
字节[]imgbytes=Convert.FromBase64String(“r0lgodlhaqabaaanvf7waaach5baeaaaaaaaaaaaaaaaaaaaaaaaaaaicraaow==”;
Response.ContentType=“image/gif”;
AppendHeader(“Content-Length”,imgbytes.Length.ToString());
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetCacheability(HttpCacheability.Public);
响应。二进制写入(imgbytes);
}
}
已请求私有布尔检查(HttpRequest请求)
{
//检查是否自标头修改,以检查接收器在过去24小时内是否已请求图像
返回请求头[“如果自”]==null?false:DateTime.Parse(请求头[“如果自”])AddHours(24)>=DateTime.Now;
}
我的index.aspx页面如下所示:

<body>
<form id="form1" runat="server">
<asp:Label ID="lblid" runat="server" Text=""></asp:Label>   
</form>
</body>

我曾经从我的aspx页面向我自己的Gmail id发送电子邮件,如:

String MailContent = "";
MailContent += "<img src='http://WWW.mywebsitename.com/index.aspx?emailId=1' width='1' height='1' />
client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("Myemailidofhmail", "mypassword");
client.Port = 25;
client.Host = "smtp.gmail.com";
client.EnableSsl = true; 
mailto = "mitesh@gmail.com";
message = MailContent.ToString();
msg = new System.Net.Mail.MailMessage();
msg.To.Add(mailto);
String MailContent=”“;
邮件内容+=”
client=新的SmtpClient();
client.UseDefaultCredentials=false;
client.Credentials=new System.Net.NetworkCredential(“myemaildofhmail”、“mypassword”);
客户端端口=25;
client.Host=“smtp.gmail.com”;
client.enablesl=true;
mailto=”mitesh@gmail.com";
message=MailContent.ToString();
msg=new System.Net.Mail.MailMessage();
msg.To.Add(mailto);

在这里,邮件将成功发送到我的Gmail帐户
mitesh@gmail.com
id,但当我从我的帐户打开该电子邮件时,我没有收到
请求。QueryString[“emailId”]
值为我的
索引.aspx
请查看这些问题的答案:

引述第一个问题的答案:

邮件客户端几乎可以阻止所有这些尝试 最好的办法是给他们一个他们希望看到的形象,如果他们 阅读信息后,他们会选择在自己的屏幕上显示图像 邮件客户端


因此,是的,这几乎是最好的建议,即使如此,如果电子邮件客户端正在缓存您的图像,它也可能无法正常工作(尽管它应该仍然能够记录第一次下载)。除非你可以让他们通过电子邮件中的链接访问你的网站,否则没有简单易行的方法来验证是否有人阅读了你的电子邮件。

跟踪电子邮件的单像素图像方案被电子邮件客户端广泛阻止。具体来说,Gmail通过代理服务器向电子邮件用户提供所有图像。你的单像素方案暗示不起作用。那么我现在还必须做什么?@Xtremcool问题不在于你的代码,而在于电子邮件客户端阻止你的图像和/或它回叫到你的服务器。如果你真的需要跟踪你的电子邮件状态,请让你的用户单击电子邮件中的链接。