简体   繁体   English

cshtml 本地图像未显示在 email html 中

[英]cshtml local image not displaying in email html

I have an email written in.cshtml with an image tag:我有一个 email 写在 .cshtml 中,带有图像标签:

<img src="../../../Images/logo.jpg">

the logo.jpg image exists in MySite\Images\logo.jpg and the email cshtml exists in MySite\Views\Mail\ContactEmail.html.cshtml logo.jpg图像存在于MySite\Images\logo.jpg和 email cshtml 存在于MySite\Views\Mail\ContactEmail.html.cshtml

However, when I intercept the email with Papercut, there is the x icon indicating that the image is missing.但是,当我用 Papercut 截取 email 时,有x图标表示图像丢失。 What could be causing the image not to load?什么可能导致图像无法加载? It works fine when using a URL in place of the image path.使用 URL 代替图像路径时,它可以正常工作。

When looking at the body of the email received in Papercut, it shows the image src as:查看 Papercut 中收到的 email 的主体时,图像 src 显示为:

<img src="../../../Images/logo.jpg">

You specified a relative path.您指定了相对路径。

Software that loads your HTML tries to request image from path: http(s)://BASE_URL/../../../Images/logo.jpg加载 HTML 的软件尝试从以下路径请求图像: http(s)://BASE_URL/../../../Images/logo.jpg

If you'd open it in browser hosted on http://somesite.com/folder/folder/folder/ContactEmail.html it would resolve your image url as: http://somesite.com/Images/logo.jpg . If you'd open it in browser hosted on http://somesite.com/folder/folder/folder/ContactEmail.html it would resolve your image url as: http://somesite.com/Images/logo.jpg .

If it was opened from local file file://C:/folder/folder/folder/ContactEmail.html it would resolve your URL to file://C:/Images/logo.jpg .如果从本地文件file://C:/folder/folder/folder/ContactEmail.html它,它会将您的 URL 解析为file://C:/Images/logo.jpg

You need to:你需要:

1) Specify absolute URL (image should be hosted somewhere!) 1)指定绝对 URL (图像应该托管在某处!)
or或者
2) You can encode your image in base64 and use it like this . 2)您可以在 base64 中编码您的图像并像这样使用它

When you view an HTML page in your browser, the browser can resolve relative paths by issuing a new HTTP request.当您在浏览器中查看 HTML 页面时,浏览器可以通过发出新的 HTTP 请求来解析相对路径。 When viewing an email, there is no URL to the HTML you're viewing, so the relative path should be resolved relative to... what?查看 email 时,没有 URL 到您正在查看的 HTML,所以相对路径应该相对于...什么?

You need to use absolute URLs in an email, inline the image or send it as an attachment.需要在 email 中使用绝对 URL,内联图像或将其作为附件发送。

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

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