Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I searched for this several times and found solutions,but all supports only one image.Finally I used the code below. But the same problem (ie),if the html contain more than one images only one image is shown in the body and the others will come as attachment.
C#
string inputHtmlContent = htmlbody;
string outputHtmlContent = string.Empty;
var myResources = new List<LinkedResource>();

if ((!string.IsNullOrEmpty(inputHtmlContent)))
{
  var doc = new HtmlAgilityPack.HtmlDocument();
  doc.LoadHtml(inputHtmlContent);
  HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//img");
  if (nodes !=null)
  {
    foreach (HtmlNode node in nodes)
    {
      if (node.Attributes.Contains("src"))
      {
        string data = node.Attributes["src"].Value;
        string imgPath = Application.StartupPath+"\\"+data;
        var imgLogo = new LinkedResource(imgPath);
        imgLogo.ContentId = Guid.NewGuid().ToString();
        imgLogo.ContentType = new ContentType("image/jpeg");
        myResources.Add(imgLogo);
        node.Attributes["src"].Value = string.Format("cid:{0}", imgLogo.ContentId);
        outputHtmlContent = doc.DocumentNode.OuterHtml;
      }
    }
  }
  else
  {
    outputHtmlContent = inputHtmlContent;
  }
  AlternateView av2 = AlternateView.CreateAlternateViewFromString(outputHtmlContent,
                            null, MediaTypeNames.Text.Html);
  foreach (LinkedResource linkedResource in myResources)
  {
    av2.LinkedResources.Add(linkedResource);
  }

  msg.AlternateViews.Add(av2);

Please help me to resolve this,How to show all images inside email body?...
Posted
Updated 16-Jan-19 5:59am
Comments
Sergey Alexandrovich Kryukov 12-Nov-15 2:56am    
This is quite solvable, but how the code shown is related to e-mail?
—SA
sayana3 12-Nov-15 2:59am    
I just shown here only message body section.
Any helpful links to display multiple images as inline.
Sergey Alexandrovich Kryukov 12-Nov-15 3:07am    
I see now, thank you. Please see my answer.
—SA

 
Share this answer
 
v2
Comments
sayana3 12-Nov-15 4:04am    
Sir,When I tried to add multiple linked resources for multiple images,its not working.Only first image is displayed ,Others just come as attached file.I need to display all images.
Sergey Alexandrovich Kryukov 12-Nov-15 10:25am    
As I said on one of the referenced answer, nothing is really "attached". This is just the matter of presentation of content in a standard mail viewer. And this is controlled by the header of each part, "content-disposition". It should be "inline" for each part you want inline.
—SA
Try using this

C#
<pre> private static AlternateView GetEmbeddedImage(string body)
        {
            var linkedResources = GetLinkedResources();

            AlternateView alternateView = AlternateView.CreateAlternateViewFromString(body, Encoding.UTF8, MediaTypeNames.Text.Html);

            foreach (var res in linkedResources)
            {
                alternateView.LinkedResources.Add(res);
            }

            return alternateView;
        }

        private static ICollection<LinkedResource> GetLinkedResources()
        {
            var linkedResources = new List<LinkedResource>();

            linkedResources.Add(new LinkedResource(@"imagepath")
            {
                ContentId = "HeaderId",
                TransferEncoding = TransferEncoding.Base64
            });

            linkedResources.Add(new LinkedResource(@"imagepath")
            {
                ContentId = "MapId",
                TransferEncoding = TransferEncoding.Base64
            });

            return linkedResources;
        }


And call the method like below

C#
mailMessage.AlternateViews.Add(GetEmbeddedImage(body));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900