Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an HTTP handler in ASP.NET to generate an image for display in email

I'm generating a barcode image as the response from an HTTP handler, like so:

public void ProcessRequest(HttpContext context)
{
    context.Response.Clear();
    context.Response.ContentType = "image/Jpeg";
    MemoryStream ms = new MemoryStream();
    Bitmap objBitmap = GenerateBarcode(context.Request.Params["Code"]);
    objBitmap.Save(ms, ImageFormat.Jpeg);
    context.Response.BinaryWrite(ms.GetBuffer());
}

I go to http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678 and it works great. Likewise I stick <img alt="" src="http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678"> on my webpage and it works great. But I stick that same image tag in an HTML email and it doesn't show up (at least not in MS Outlook 2007; I haven't tested other email clients yet.)

I'm guessing this is somehow related to the fact that I'm using an HTTP handler, as other, static images in the email are showing up fine. How can I fix this so that the image shows up? (I can't just use a static image because the code is determined at the time the email is sent.)

Update:

It turns out I hadn't noticed a key detail. The image isn't just not showing up; rather the image src attribute is getting replaced with "http://portal.mxlogic.com/images/transparent.gif". I've determined that using the .aspx or .ashx extensions triggers this replacement (or probably any extension except those expected for images like .gif or .jpg), and that including a query string in the URL also triggers this, even when it's a standard image extension. I guess it's some overzealous security feature. So including an image like BarCode.aspx?code=12345678 just isn't going to work.

It occurs to me that I could do something like <img alt="" src="http://www.MyWebsite.com/MyProject/12345678/BarCode.jpg"> and then create a handler for all files named BarCode.jpg. Here 12345678/ isn't an actual path but it wouldn't matter since I'm redirecting the request to a handler, and I could scrape the code value from that phony path in the URL. But, I'd probably have to change some IIS setting to have requests for .jpg files handled by ASP.NET, and I'd want to still make sure that other JPEGs besides my BarCode.jpg loaded normally.

Honestly, I'm not sure if it's worth the hassle.

like image 610
Tim Goodman Avatar asked Sep 11 '10 21:09

Tim Goodman


People also ask

How do I display an image in ASPX?

If an image is already available on your site, then you can use HTML <img> tag to display it on a page.

What is HTTP handler in ASP.NET MVC?

“ - An HTTP handler is the process, also called the endpoint, that runs in response to a request. It's used to handle specific requests based on file extensions and verbs. You can leverage these handlers to inject processing logic into the request pipeline.

What is HTTP handler in ASP.NET core?

In an ASP.NET web application, the HTTP handler is a process that is executed on each response to the requests made to the web server. We can create our own custom HTTP handlers to render desired output. Handler class for redirection. The following is the code to redirect all the . aspx extension pages to a new page.

What is the use of httpHandlers when to use this?

An HTTPhandler may be defined as an end point that is executed in response to a request and is used to handle specific requests based on extensions. The ASP.Net runtime engine selects the appropriate handler to serve an incoming request based on the file extension of the request URL.


2 Answers

Microsoft Office Outlook 2007 uses the HTML parsing and rendering engine from Microsoft Office Word 2007 to display HTML message bodies. more

which leaves us with some hairy points

The limitations imposed by Word 2007 are described in detail linked off in the article, but here are a few highlights:

  • no support for background images (HTML or CSS)
  • no support for forms
  • no support for Flash, or other plugins
  • no support for CSS floats
  • no support for replacing bullets with images in unordered lists
  • no support for CSS positioning
  • no support for animated GIFs

i think your best bet would be to embed the image using the LinkedResource class, something like

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("...");
mail.To.Add("...");

//set the content
mail.Subject = "Test mail";

AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image." ", null, "text/html");

//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource(GenerateBarcode(Code)));
logo.ContentId = "logo";
htmlView.LinkedResources.Add(logo);

mail.AlternateViews.Add(htmlView);

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); 
smtp.Send(mail);
like image 126
almog.ori Avatar answered Sep 28 '22 20:09

almog.ori


I don't know why it doesn't work but if you are saying that static images from the site hosting the handler work fine here's a slightly modified version you might try:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "image/jpeg";
    using (var image = GenerateBarcode(context.Request.Params["Code"]))
    {
        image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }
}
  1. No need to clear the response in a generic handler as there's nothing written yet
  2. Content-Type is image/jpeg
  3. No need of intermediary memory stream, you could write directly to the response
  4. Make sure to properly dispose the bitmap
like image 38
Darin Dimitrov Avatar answered Sep 28 '22 18:09

Darin Dimitrov