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.