Question

I have searched many articles and have found some code, but it's not working for me.

My index.aspx.cs page code is:

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

My index.aspx page seems as:

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

I used to send email to my own Gmail id from my aspx page like:

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

Here the mail is coming successfully to my Gmail account on mitesh@gmail.com id, but when I open that email from my account I am not getting Request.QueryString["emailId"] value as 1 to my index.aspx

Was it helpful?

Solution

Please see the answers to these questions:

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

Mass email tracking

Quote from the answer in the first question:

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.

So yeah, that's pretty much the best advice, and even then it may not work well if the email client is caching your images (though it should still be able to log the first download). There's no foolproof method of actually verifying if someone's read your email, unless you can make them visit your website with a link in the email.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top