blogs

blogs

03.242017
1 0
Get email delivery and read receipt by HttpModule

Get email delivery and read receipt by HttpModule

Introduction:

Email is very important in many web application and web technologies. Sending email is easy but tracking email status i.e. email is delivered to recipient or not it’s a difficult part. Email tracking is useful when the sender wants to know if the recipient actually received the email.

We can track email status by implementing HttpModule. In this method image is wrapped with HTML composed email. This HttpModule will intercept a request to embedded image. This image is act as an email key which is unique for each email. That image has SRC attribute which is pointing to another external file, it will look like below

protected void btnSendMail_Click(object sender, EventArgs e)
 {
   // a unique-id to attach with the mail
var messageId = Guid.NewGuid();
   // the server get request from the image and process it
var externalfile= "http://www.yoursite.com//images//MyDemo.aspxf?key=textkey";
   // the image tag with "src" pointing to the external resource
  // passing necessary parameter and unique-id in the query-string.
   var imgTag =
   string.Format(@"
   style=""width: 0px; height: 0px; border:0px;"" />",
   externalfile, messageId);
   System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage("sender@mainhost.com", "receiver@subhost.com")
   oMail.Subject = "Email delivery using HttpModule";
   oMail.Body = "An image is attached with this email, please enable the images on reading the email so we will know you got the email.";
   //append the image tag at the end of the body.
  oMail.Body += imgTag;
  oMail.IsBodyHtml = true;
            SmtpClient sm = new SmtpClient("smtp.gmail.com");
            sm.Port = 587;
            sm.DeliveryMethod = SmtpDeliveryMethod.Network;
            sm.Credentials = new System.Net.NetworkCredential("userid", "password");
            sm.EnableSsl = true;
    }

 Using above code sender will send email to receiver

 public class Request1 : IHttpModule 
{
  private string pattern = @"/images/(?.*).aspx";
  private string logoFile = "~/logo.jpg";

        public Request1()
        {
        }
        public void Dispose() 
        {
        }
        public void Init(System.Web.HttpApplication Appl) 
        {
                   Appl.BeginRequest += new System.EventHandler(GetImage_BeginRequest);
        }
         public void GetImage(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

                   //create the regex to match for becon 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);
                        GetKey(key);
                     }

                     //now send the image to the client
                    application.Response.ContentType = "image/gif";
                    application.Response.WriteFile(application.Request.MapPath(logoFile));

                     //end the resposne
                    application.Response.End();
                  }
              }

         private void GetKey( string key )
        {
                     if ((key ==  null) || (key.Trim().Length == 0))
                         return;
        }
        }

When receiver try to read email then GetImage() is called. Inside this method it check the path and run the expression http://www.yoursite.com//images//MyDemo.aspx ,it will return email key. That key is then passed to the the GetKey method ,here we can save this key into database. Using this key later we can determine which email were read and it will return Image as acknowledgement.

Once we done with above HttpModule we have to deploy it, for that some entries need to do in web.config file as below:

  <system.web>
     <httpModules>
       <add type="LoadImage.Request1,LoadImage" name="LoadImage" />
     </httpModules>
  </system.web>

Limitation:

1. This mechanism will not work for gmail.

Leave a Reply

Your email address will not be published. Required fields are marked *