在 ASP.NET 中使用 HTTP 处理程序生成图像以在电子邮件中显示
我正在生成条形码图像作为 HTTP 处理程序的响应,如下所示:
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());
}
我转到 http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678 效果很好。同样,我将 粘贴在我的网页上,效果很好。但我在 HTML 电子邮件中粘贴了相同的图像标签,但它没有显示(至少在 MS Outlook 2007 中没有显示;我还没有测试过其他电子邮件客户端。)
我猜测这在某种程度上与以下事实有关:我正在使用 HTTP 处理程序,因为电子邮件中的其他静态图像显示正常。我该如何解决这个问题以使图像显示出来? (我不能只使用静态图像,因为代码是在发送电子邮件时确定的。)
更新:
事实证明我没有注意到一个关键细节。图像不仅没有显示出来,而且还没有显示出来。相反,图像 src 属性被替换为“http://portal.mxlogic.com/images/透明.gif”。我确定使用 .aspx 或 .ashx 扩展名会触发此替换(或者可能是除 .gif 或 .jpg 等图像所需的任何扩展名),并且在 URL 中包含查询字符串也 触发此操作,即使它是标准图像扩展。我猜这是一些过于热心的安全功能。因此,包含像 BarCode.aspx?code=12345678 这样的图像是行不通的。
我想到我可以做类似 的事情,然后创建所有名为 BarCode.jpg 的文件的处理程序。这里 12345678/ 不是实际路径,但这并不重要,因为我将请求重定向到处理程序,并且我可以从 URL 中的虚假路径中抓取代码值。但是,我可能需要更改一些 IIS 设置才能让 ASP.NET 处理对 .jpg 文件的请求,并且我仍然希望确保加载除 BarCode.jpg 之外的其他 JPEG通常情况下。
老实说,我不确定这是否值得这么麻烦。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Microsoft Office Outlook 2007 使用 Microsoft Office Word 2007 中的 HTML 解析和呈现引擎来显示 HTML 邮件正文。 更多
这给我们留下了一些棘手的问题。Word
2007 施加的限制在 文章,但这里有一些亮点:
(HTML 或 CSS)
插件
无序列表中的图像
动画 GIF
我认为最好的选择是使用 LinkedResource 类嵌入图像,例如
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:
(HTML or CSS)
plugins
images in unordered lists
animated GIFs
i think your best bet would be to embed the image using the LinkedResource class, something like
我不知道为什么它不起作用,但如果您说托管处理程序的站点中的静态图像工作正常,您可以尝试以下稍微修改的版本:
image/jpeg
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:
image/jpeg
您必须将图像作为附件,或在邮件中使用 html。不过,您的错误可能是由于 Outlook 造成的。
You will have to put the image as an attachment, or use html in the mail. Probably your error happends due to Outlook though.
此链接表明了问题,正如您所建议的那样导致问题的安全/反垃圾邮件功能(mxlogic.com 指向 McAfee 产品)
This link suggests the problem, as you suggested is a security/anti-SPAM feature causing the problem (mxlogic.com points to a McAfee product)