Click here to Skip to main content
Click here to Skip to main content

Get Notified When Client Opens Email

By , 15 Jan 2009
 

Introduction

So, you had a fight with your girlfriend and she is not taking your phone calls. You left countless messages but she never replied. You sent her emails but have no idea that if she checked them or not. In these situations it will be a good idea to get a notification when your girlfriend has opened her email. This will give you a little piece of mind that at least she is reading your emails. Let's see how we can implement this functionality.

Basic Idea

The basic idea is to embed a very small invisible image in the email. The image will contain a GUID as a query string. The image is downloaded by the client which makes a request back to the server and sends the query string. The query string is extracted and the field in the database is updated indicating that the user has checked the email.

Creating an Invisible Image

You can create a very small image with a white background. This can be as small as a dot. In this article we have made the image larger so that you will have a better idea of what we are talking about. Take a look at the image below:

EmailNotification_1.PNG

The image above also contains the red dot. The red dot is only used so that you will have a better idea where the image is displayed. In actual production code you will use a much smaller image.

Attaching a GUID with Image

The next step is to attach a Guid as a query string with the image. In production code this Guid will come from a database table which will be used to track which user has checked their email and which has not but in this case we will be creating a dummy Guid. Take a look at the following code which attaches a Guid with the image URL as a query string parameter.

  protected string GetImageUrl()
        {
            return String.Format(<a href="%22http://localhost:1404/WhiteImage.png?id={0}"">http://localhost:1404/WhiteImage.png?id={0}</a>,
            Guid.NewGuid().ToString());
        }

And here is the ASPX code that uses the GetImageUrl method.

 <form id="form1" runat="server">
    <div>
    
    This is some email that I am reading. This is a test email. 
    
    <img src='<%= GetImageUrl() %>' />
    
    </div>
    </form>

The HTML produced by the above code will look something like this:

EmailNotification_2.PNG

Our next task is to handle the request from our WhiteImage.png image. The best way to handle this request is to create a custom HttpHandler.

Creating EmailViewedNotificationHandler

The purpose of EmailViewedNotificationHandler is to intercept the request for WhiteImage.png and then update a field in the database and finally render the image to the browser. The ProcessRequest method is where the magic happens. Let's take a look at the ProcessRequest implementation.

 public void ProcessRequest(HttpContext context)
        {
            string id = context.Request.QueryString["id"] as String;

            if (String.IsNullOrEmpty(id)) return;

            Guid guid = new Guid(id);

            _emailNotificationService.MarkEmailRead(guid); 

            // render the image!
            MemoryStream ms = new MemoryStream(); 
            Bitmap bitMap = new Bitmap(context.Request.PhysicalPath);
            bitMap.Save(ms, ImageFormat.Png);

            byte[] buffer = ms.ToArray();

            context.Response.Clear();
            context.Response.ContentType = "image/png";
            context.Response.BinaryWrite(buffer);
            context.Response.End(); 
        }

The ProcessRequest method first extracts the query string and gets the Guid. Then the EmailNotificationService class is used to update the Guid status in the database. This class is not implemented in the article. Finally, the image is written to the MemoryStream which is outputted to the client.

SmallImage.PNG

Adding HttpHandler Configuration in Web.Config

In order to work with the new HttpHandler you will need to register it in the Web.config file. Take a look at the following code that does this:

<httpHandlers>
            <remove verb="*" path="*.asmx"/>

      <add verb="*" path="WhiteImage.png"
         type="LearningWebApplication.EmailViewedNotificationHandler"/>            
        </httpHandlers>

You might also want to adjust your FileMappings in IIS to work with the new HttpHandler.

Conclusion

In this article we learned how to create a simple HttpHandler that will notify the sender when the email is opened. This can be useful in scenarios where viewing of an email is absolutely necessary.

NOTE: If the client has an image blocking filter active then the image will never be downloaded and the above code will not work.

UPDATE: You can also use the .GIF transparent images

References

  1. Sending Emails in ASP.NET 2.0
  2. Sending Emails in ASP.NET

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

azamsharp
Web Developer
United States United States
Member
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.
 
HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.
 
HighOnCoding.com www.HighOnCoding.com
 

My Blog:

Blog

 

Buy my iPhone app ABC Pop

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
RantMy vote of 1memberJonathan C Dickinson20 Jan '09 - 19:33 
The less spammers have this type of stuff the better. As far as I see it there is NO value for this type of thing outside the spamming/email harvesting industry. Can my company send you their internet bills? Will YOU pay for our wasted email bandwidth?
 
Even if you did come up with a good use, email clients blocking this mitigates the usefulness.
 
Do us all a favor and pull this article down, because all-in-all the article is a 'hacker script' and probably a useless one at that.
 
I actually really wish you could vote an article 0.
 
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb]
 
Jonathan C Dickinson (C# Software Engineer)

GeneralRe: My vote of 1memberJonathan C Dickinson20 Jan '09 - 19:39 
And by the way, there are two things that can be used here. A delivery receipt and a read receipt. Most hosts will automatically respond when delivered. Most clients will ask about the read receipt. To me a read receipt is not a violation of privacy (as is your method) - as the other end can send it if they want.
 
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb]
 
Jonathan C Dickinson (C# Software Engineer)

GeneralRe: My vote of 1memberSamNaseri5 Dec '11 - 18:32 
I also think about adding a link into the email, so the email reader could click it to confirm that he has read the email. Exactly the same as common technique for confirming email addresses.
GeneralInteresting introductionmemberChiew Heng Wah19 Jan '09 - 18:55 
I give a 3 for the uncommon introduction approach.
GeneralMy vote of 1memberMatt Sollars19 Jan '09 - 6:04 
Email clients block images!
GeneralIn most cases this will workmemberdefwebserver19 Jan '09 - 2:53 
People get emails with pictures all of the time. Yes your Outlook email will block the images but it allows you to click a button to display them.
 
So instead of a red dot you should send an image with an "Alt Tag" like "cute puppies" (and actually have an image of cute puppies).
GeneralRe: In most cases this will workmemberazamsharp19 Jan '09 - 3:28 
I agree with you!
 
The red dot is only shown to shown for this article. In actual code you will have a transparent image. Also, at the end of the article I have clearly mentioned that this solution will not work for all clients.
 
Mohammad Azam
azamsharp@gmail.com
www.gridviewguy.com
www.screencastaday.com
Houston, TEXAS

GeneralMy vote of 1memberWolfgangSchober19 Jan '09 - 0:48 
most email clients block images!
GeneralMy vote of 1memberMichael B. Hansen16 Jan '09 - 6:37 
Oldest spammer trick in the book
GeneralRe: My vote of 1memberazamsharp16 Jan '09 - 8:34 
I have not seen any article on this that is why I implemented it.
 
Mohammad Azam
azamsharp@gmail.com
www.gridviewguy.com
www.screencastaday.com
Houston, TEXAS

RantRe: My vote of 1memberJonathan C Dickinson20 Jan '09 - 19:34 
Oh what, so next there is no article on writing a worm/virus? I bet that would be a great article to read as well.
 
In fact, write it, then I can report you to the feds.
 
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb]
 
Jonathan C Dickinson (C# Software Engineer)

Generalgood ideamemberHaozes15 Jan '09 - 21:04 
hehe
 
welcome to my blog:http://solo.cnblogs.com

GeneralMy vote of 1memberJ@@NS15 Jan '09 - 20:25 
1. Image will be blocked.
2. Most of the email clients will open mails as plain text.
GeneralRe: My vote of 1memberazamsharp16 Jan '09 - 2:11 
I have mentioned this in my article!
 
Mohammad Azam
azamsharp@gmail.com
www.gridviewguy.com
www.screencastaday.com
Houston, TEXAS

GeneralMy vote of 1memberRogic15 Jan '09 - 14:15 
lot of mail clients block image downloading by default
Jokeimprove :)memberteleprobst-ober15 Jan '09 - 10:24 
cause a lot of mail clients block image downloading by default, tell your girlfriend that there's an image! so she clicks on "show images" to actually see it.
 
and better use e.g. mod_rewrite to not obviously show that youre using a guid.
 
edit: mod_rewrite on apache of course, you can do it with a php file

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 15 Jan 2009
Article Copyright 2009 by azamsharp
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid