views:

107

answers:

3

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"&gt; 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"&gt; 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.

A: 

You will have to put the image as an attachment, or use html in the mail. Probably your error happends due to Outlook though.

Femaref
He mentions that static images are showing fine.
Darin Dimitrov
Ah, but how does he define a "static image"?
spender
I guess a one that is not generated dynamically from a generic handler but read directly from the disk.
Darin Dimitrov
Yes, the ones which have `<img src="SomeActualFileOnMyDomain">` work fine, just not the one generated from my handler. I recognize that some people will have all images blocked, but I want the handler-generated one to work at least as well as the others.
Tim Goodman
+3  A: 

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
Darin Dimitrov
Thanks, I'll give this a try.
Tim Goodman
+5  A: 

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);
almog.ori
Thanks, I'll look into this.
Tim Goodman