0

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

  • 1
    I suspect you might need to [embed your image](https://stackoverflow.com/questions/9110091/base64-encoded-images-in-email-signatures) into the email. – timur Aug 27 '20 at 09:36
  • 1
    HTML emails cannot access local-computer resources for what should be obvious security and privacy reasons. Instead, you you need to embed the image within the email as a MIME multi-part entry with a `cid:` URI. – Dai Aug 27 '20 at 09:37
  • 1
    You could embed your image as timur said , but be careful embedded images aren't supported in Outlook and many other web apps/clients. I think that the Dai comment may suit your needs. – MatteoCracco97 Aug 27 '20 at 09:43
  • @dai. Thanks, for the answer, makes more sense now. Tahnks to all that helped :- –  Aug 27 '20 at 10:16

1 Answers1

0

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;
Sowmyadhar Gourishetty
  • 1,843
  • 1
  • 8
  • 15
Mcan_Cicek
  • 234
  • 2
  • 7