c#/ html

I a struggling to get the image display from the local resource folder in the html email body.

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

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;

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

here is the method for convert to 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;

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question cshtml local image not displaying in email html displaying email body as html in c# textbox Fetch only text from email html body Displaying list of values from a list view into body of an email Displaying message body from MIME encoded email string Outlook email picture attachment not showing, when I displaying outlook html email body in asp.net page Issue displaying a local image from XAML Sending email with html body true having image using wcf service HTML Email Body Base64 Image not showing set image in email body using html c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM