[英]Create HTML img tag in ASP.NET code-behind using C#
I am trying to create an img
tag in code-behind and send it in email (in the body of the email not as attachment). 我正在尝试在代码隐藏区中创建一个img
标签,并通过电子邮件发送(在电子邮件正文中而不是附件中)。
body += "<p><img src='" + imageUrl.Remove(0,2) + "' alt='Product Image' width='250px' height='250px' runat='server' /></p>";
NB: I have declared body
as string and I am adding other HTML controls to it (eg p,h1,li etc). 注意:我已将body
声明为字符串,并向其中添加了其他HTML控件(例如p,h1,li等)。
The imageUrl
variable returns "~/Images/bag/name_of_image.jpg" hence I am removing the first two characters which are ~/. imageUrl
变量返回“〜/ Images / bag / name_of_image.jpg”,因此我要删除前两个字符,即〜/。
It seems like it is creating the image but its not displaying in the email body. 似乎正在创建图像,但未在电子邮件正文中显示。
You need include the absolute path to the image in the e-mail. 您需要在电子邮件中包含图像的绝对路径。
Images embedded in an email should be like: 电子邮件中嵌入的图像应类似于:
<img src="http://www.example.com/image_link.jpg" alt="Some Image" />
You however in your email you would be embedding: 但是,您将在电子邮件中嵌入:
<img src="image_link.jpg" alt="Some Image" />
As the e-mail application the user has won't have a copy of image_link.jpg
it can't show it. 作为电子邮件应用程序,用户将没有image_link.jpg
的副本,因此无法显示它。
So in your process you may need to upload the image to an externally accessible webserver (if being access outside your network) or at least an internally accessible web server. 因此,在您的过程中,您可能需要将图像上传到外部可访问的Web服务器(如果可以从网络外部访问)或至少内部可访问的Web服务器。
I would suggest to use embedded image in your email body. 我建议您在电子邮件正文中使用嵌入式图像。 Have a look at below URLs: 看看下面的URL:
Please do it as below. 请按照以下步骤进行。 just remove "~" from SRC. 只需从SRC中删除“〜”即可。
string mainUrl = "http://testURL.com/"; //its just example, you can put this in web config as well and than retrive it
body += "<p><img src='" + mainUrl + imageUrl.Remove(0,1) + "' alt='Product Image' width='250px' height='250px' runat='server' /></p>";
Hope this will help you, 希望这个能对您有所帮助,
Thanks 谢谢
If you embed an image with its local path to an e-mail, this image cannot be accessible from remote receviers. 如果您将图像及其本地路径嵌入到电子邮件中,则无法从远程接收器访问该图像。
It is basic; 这是基本的; user opens your e-mail using with any e-mail client program (outlook, thunderbird or webmail etc.), and e-mail client program searches this image using with its path. 用户使用任何电子邮件客户端程序(外观,雷鸟或网络邮件等)打开您的电子邮件,然后电子邮件客户端程序使用其路径搜索该图像。 If path is a local, how can e-mail client program access this image? 如果path是本地的,则电子邮件客户端程序如何访问此图像?
You have to give a common accessible path that e-mail clients can access your image file and load then show it to user. 您必须提供一个公共的可访问路径,电子邮件客户端可以访问该路径并加载您的图像文件,然后将其显示给用户。
So upload your image to a server, then use the server address to specify image path. 因此,将图像上传到服务器,然后使用服务器地址指定图像路径。
An example: 一个例子:
string domainName = @"http://lindaLinda.com/images/";
string imageFileName = "myFirstEmailImg.jpg";
body += @"<p><img src='" + domainName + imageFileName +"' alt='Product Image' width='250px' height='250px' runat='server' /></p>";
It gives this output: 它给出以下输出:
<p><img src='http://lindaLinda.com/images/myFirstEmailImg.jpg' alt='Product Image' width='250px' height='250px' runat='server' /></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.