简体   繁体   English

图像未显示在来自本地资源的 html 电子邮件正文中

[英]Image is not displaying in html email body from local resource

I a struggling to get the image display from the local resource folder in the html email body.我正在努力从 html 电子邮件正文中的本地资源文件夹中获取图像显示。

This is the code I am using:这是我正在使用的代码:

string htmlBody;
htmlBody = "<h2> Hi Custom Designs, </h2>" +
           "Please see below for new design request" + "<br /><br />" +
           "<strong>Name:</strong>          " + txtName.Text + "<br />" +
           "<strong>Last Name:</strong>     " + txtLastName.Text + "<br />" +
           "<strong>Email Address:</strong> " + txtEmail.Text + "<br />" +
           "<strong>Phone Number:</strong>  " + txtCell.Text + "<br />" +
           "<strong>Address:</strong>       " + txtAddress.Text + "<br />" +
           "<strong>Message:</strong>       " + txtMessage.Text + 
           "<br /><br /><br />" +
           "<img src='assets/img/logo.png' alt='Logo' title='Logo' style='display:block' width='200' height='87' />" + "<br /><br />" +
           "Thanks" ;
                
            mail.Body = htmlBody;

I have tried "<img src='../assets/img/logo.png' alt='Logo' title='Logo' style='display:block' width='200' height='87' />" and "<img src='~/assets/img/logo.png' alt='Logo' title='Logo' style='display:block' width='200' height='87' />" but still not working and the image is definitely in that folder我试过"<img src='../assets/img/logo.png' alt='Logo' title='Logo' style='display:block' width='200' height='87' />""<img src='~/assets/img/logo.png' alt='Logo' title='Logo' style='display:block' width='200' height='87' />"但仍然没有工作并且图像肯定在该文件夹中

When the email is received is says that the image can't be displayed due to it has been remove or link is incorrect or deleted?收到邮件时说图片已被删除或链接不正确或删除而无法显示?

Not sure what am doing wrong here?不确定这里做错了什么?

Thanks谢谢

You cant access your local data when send a Email.发送电子邮件时您无法访问本地数据。 You should convert image to base64 before to use, and then you should add your e-mail body like this;使用前应该先将图片转为base64,然后像这样添加邮件正文;

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

here is the method for convert to base 64;这是转换为 base 64 的方法;

byte[] imageArray = System.IO.File.ReadAllBytes(@"image file path");
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
var imgSrc = String.Format("data:image/png;base64,{0}", base64ImageRepresentation);//src data

and change your code like this;并像这样更改您的代码;

string htmlBody;
htmlBody = "<h2> Hi Custom Designs, </h2>" +
           "Please see below for new design request" + "<br /><br />" +
           "<strong>Name:</strong>          " + txtName.Text + "<br />" +
           "<strong>Last Name:</strong>     " + txtLastName.Text + "<br />" +
           "<strong>Email Address:</strong> " + txtEmail.Text + "<br />" +
           "<strong>Phone Number:</strong>  " + txtCell.Text + "<br />" +
           "<strong>Address:</strong>       " + txtAddress.Text + "<br />" +
           "<strong>Message:</strong>       " + txtMessage.Text + 
           "<br /><br /><br />" +
           "<img src='" + imgSrc + "' alt='Logo' title='Logo' style='display:block' width='200' height='87' />" + "<br /><br />" +
           "Thanks" ;
                
            mail.Body = htmlBody;

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

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