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

I am creating a barcode image as a response from an HTTP handler, for example:

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 fine. Similarly, I stick with <img alt="" src="http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678"> on my web page, and it works great. But I stick to the same image tag in the HTML email and it does not appear (at least not in MS Outlook 2007, I have not tested other email clients yet.)

I suppose this has something to do with the fact that I am using an HTTP handler, as other static images in the email render well. How can I fix this so that the image appears? (I can't just use a static image because the code is detected when sending email.)

Update:

Turns out I didn't notice a key detail. The image is not just not displayed; rather, the src attribute of the image is replaced by " http://portal.mxlogic.com/images/transparent.gif ". I decided that using the .aspx or .ashx extensions would cause this replacement (or perhaps any extension, except for those expected for images like .gif or .jpg), and that including the query string in the URL would also cause this. even if it's a standard image extension. I assume this is an excessive security feature. Therefore including an image such as BarCode.aspx? Code = 12345678, just won't work.

It seems 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 with the name BarCode.jpg. Here 12345678 / is not the actual path, but that would not matter, as I redirect the request to the handler, and I could clear the code value from this logical path in the URL. But I probably have to change some IIS setting to have requests for .jpg files handled by ASP.NET, and I want it to still check that other JPEGs other than my BarCode.jpg load normally.

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

+6
source share
4 answers

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

what leaves us with hairy dots

The limitations imposed by Word 2007 are described in detail in the article , but here are a few key points:

  • support for background images (HTML or CSS)
  • form support
  • no support for flash or other plugins
  • CSS flag support
  • No support for replacing bullets from an image in disordered lists
  • no CSS positioning support
  • no support for animated gifs

I think it’s best 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); 
+5
source

I don’t know why this does not work, but if you say that static images from the site hosting the handler work fine here, a slightly modified version that you can 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); } } 
  • No need to clear the response in the general handler, since nothing is written
  • Content-Type image/jpeg
  • There is no need for an intermediate memory stream, you can directly write the answer
  • Be sure to adjust the bitmap
+3
source

You will need to place the image as an attachment or use html in the letter. Your error is probably due to Outlook though.

0
source

All Articles