C# 如何在 .NET HTML 邮件消息中嵌入图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/104177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:08:12  来源:igfitidea点击:

How do I embed an image in a .NET HTML Mail Message?

提问by

I have an HTML Mail template, with a place holder for the image. I am getting the image I need to send out of a database and saving it into a photo directory. I need to embed the image in the HTML Message.

我有一个 HTML 邮件模板,带有图像的占位符。我正在获取需要从数据库中发送的图像并将其保存到照片目录中。我需要将图像嵌入 HTML 消息中。

I have explored using an AlternateView:

我已经使用 AlternateView 进行了探索:

AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<HTML> <img src=cid:VisitorImage> </HTML>");

LinkedResource VisitorImage = new LinkedResource(p_ImagePath);
VisitorImage.ContentId= "VisitorImage";
htmlView.LinkedResources.Add(VisitorImage);

采纳答案by Scott and the Dev Team

Try this:

尝试这个:

LinkedResource objLinkedRes = new LinkedResource(
            Server.MapPath(".") + "\fuzzydev-logo.jpg", 
            "image/jpeg");
objLinkedRes.ContentId = "fuzzydev-logo";       
AlternateView objHTLMAltView = AlternateView.CreateAlternateViewFromString(
            "<img src='cid:fuzzydev-logo' />", 
            new System.Net.Mime.ContentType("text/html"));
objHTLMAltView.LinkedResources.Add(objLinkedRes);
objMailMessage.AlternateViews.Add(objHTLMAltView);