简体   繁体   English

在ASP.NET中使用HTTP处理程序生成要在电子邮件中显示的图像

[英]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: 我正在生成条形码图像作为来自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());
}

I go to http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678 and it works great. 我去了http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678 ,效果很好。 Likewise I stick <img alt="" src="http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678"> on my webpage and it works great. 同样,我在我的网页上粘贴了<img alt="" src="http://www.MyWebsite.com/MyProject/BarCode.aspx?code=12345678"> ,效果很好。 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.) 但是我在HTML电子邮件中粘贴了相同的图像标记并且没有显示(至少在MS Outlook 2007中没有显示;我还没有测试过其他电子邮件客户端。)

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. 我猜这在某种程度上与我使用HTTP处理程序这一事实有关,因为电子邮件中的其他静态图像显示正常。 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 ". 相反,图像src属性被替换为“ 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. 我已经确定使用.aspx或.ashx扩展名触发了这个替换(或者可能是任何扩展名,除了那些预期用于.gif或.jpg之类的图像),并且包含URL中的查询字符串触发了这个,即使它是标准图像扩展。 I guess it's some overzealous security feature. 我想这是一些过于热心的安全功能。 So including an image like BarCode.aspx?code=12345678 just isn't going to work. 所以包括像BarCode.aspx?code = 12345678这样的图像是行不通的。

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. 我发现我可以执行类似<img alt="" src="http://www.MyWebsite.com/MyProject/12345678/BarCode.jpg"> ,然后为名为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. 这里12345678 /不是一个实际的路径,但是因为我将请求重定向到处理程序,所以我可以从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. 但是,我可能不得不改变一些IIS设置来处理ASP.NET处理的.jpg文件的请求,并且我还想确保除了我的BarCode.jpg之外的其他 JPEG正常加载。

Honestly, I'm not sure if it's worth the hassle. 老实说,我不确定这是否值得麻烦。

Microsoft Office Outlook 2007 uses the HTML parsing and rendering engine from Microsoft Office Word 2007 to display HTML message bodies. Microsoft Office Outlook 2007使用Microsoft Office Word 2007中的HTML解析和呈现引擎来显示HTML邮件正文。 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: Word 2007所施加的限制在文章中有详细描述,但这里有一些亮点:

  • no support for background images (HTML or CSS) 不支持背景图像(HTML或CSS)
  • no support for forms 不支持表格
  • no support for Flash, or other plugins 不支持Flash或其他插件
  • no support for CSS floats 不支持CSS浮动
  • no support for replacing bullets with images in unordered lists 不支持用无序列表中的图像替换子弹
  • no support for CSS positioning 不支持CSS定位
  • no support for animated GIFs 不支持动画GIF

i think your best bet would be to embed the image using the LinkedResource class, something like 我认为你最好的选择是使用LinkedResource类嵌入图像 ,类似于

//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);

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 Content-Type是image/jpeg
  3. No need of intermediary memory stream, you could write directly to the response 不需要中间内存流,您可以直接写入响应
  4. Make sure to properly dispose the bitmap 确保正确处理位图

链接表明问题,因为您建议使用安全/反垃圾邮件功能导致问题(mxlogic.com指向McAfee产品)

You will have to put the image as an attachment, or use html in the mail. 您必须将图像作为附件,或在邮件中使用html。 Probably your error happends due to Outlook though. 可能你的错误是由于Outlook而发生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM