Hennie Francis
Hennie Francis

Reputation: 124

Image in Email Body

I am building a e-mail body up with a StringBuilder. I need to insert a image at the end of the e-mail as a signature.

My image is located a folder in my MVC solution. The source to the image I can browse, and that works.

if I do browse to: http://localhost/Content/images/CustomerSatisfaction.jpg, I get the image returned, just now to get this image as part of the body of the email

string imgSrc = string.Format("@{0}{1}", HttpContext.Current.Request.Url.Authority,
"/Content/images/CustomerSatisfaction.jpg");

StringBuilder sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("<body>");
sb.AppendLine(string.Format("<img src='{0}' alt='xxx'>", imgSrc));
sb.AppendLine("</body>");
sb.AppendLine("</html>");

I have also tried several versions for my image source:

string imgSrc = "~/Content/images/CustomerSatisfaction.jpg";

Upvotes: 1

Views: 2704

Answers (1)

Gortonington
Gortonington

Reputation: 3587

Images need to be hosted externally inside emails or embedded.

I would recommend external hosting as it is the easiest to do and allows for lightweight emails (potential increase in deliverability). Embedded images are not displayed in some email clients, so that may cause issues as well.

See this article for some good in-depth info into External link vs. Embedding images. https://sendgrid.com/blog/embedding-images-emails-facts/

Below is quick synopsis of article:

CID Embedded Images (Inline Images)

Pros

  • It’s been around for a long time
  • Usage ensures the corrent mime-type of multiplart/related

Cons

  • Increases the size of overall email
  • Varying results across email clients, especially webmail
  • Feels outdated
  • Lots more effort
  • Harder for less technical staff to achieve

Inline Embedding (Base64 Encoding)

Pros

  • Much simpler to achieve
  • Much faster to do
  • Requires much less deep dive into MIME and application code

Cons

  • Can really increase size of emails especially if you use more than one image
  • Most likely blocked by default in many webmail services
  • Blocked completely in Outlook

Linked Images

Pros

  • Keeps email weight light
  • Requires very little extra effort
  • Allows for changes to images after sending

Cons

  • Suffers the same blocking problems as base64 encoding on most services
  • Requires download from external servers

Upvotes: 2

Related Questions