加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:01:40 所属栏目:百科 来源:网络整理
导读:我搜索了很多文章并找到了一些代码,但它对我不起作用. 我的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
我搜索了很多文章并找到了一些代码,但它对我不起作用.

我的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;
}

我的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);

这里的邮件已成功发送到我的Gmail帐户mitesh@gmail.com id,但是当我从我的帐户打开该电子邮件时,我没有将Request.QueryString [“emailId”]值作为1传递给我的index.aspx

解决方法

请查看以下问题的答案:

Is there any way to track whether an email has been opened?

Mass email tracking

引用第一个问题的答案:

Mail clients block pretty much all of these kinds of attempts. The
best idea is to give them an image that they would want to see if they
read the message,and therefore they choose to display images in their
mail client.

所以,是的,这几乎是最好的建议,即使这样,如果电子邮件客户端缓存您的图像(尽管它仍然能够记录第一次下载),它可能无法正常工作.没有万无一失的方法来实际验证某人是否阅读了您的电子邮件,除非您可以通过电子邮件中的链接访问您的网站.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读