Embedded image into Email Template using Byte() data

peter liles 556 Reputation points
2023-05-03T09:16:20.0433333+00:00
   I am able to embed a image held on disk like below but now i want to embed a image held on database and is stored as a Byte() in a  collection List(of Orders) 
i.e  Dim myImage As Byte() = l.Data

Somehow i need to add this to LinkedResource ?

-----------------------------------------------------------------------
 Dim theEmailImage As LinkedResource = Nothing
        For Each l As Orders In orderslist


            theEmailImage = New LinkedResource(Server.MapPath("~/MyProject/image/Nectar_logo.png"), System.Net.Mime.MediaTypeNames.Image.Jpeg)

   theEmailImage.ContentId = "myImageID"

  textBody += "<tr><td>" + String.Format(" <img src='cid:{0}'   alt='Logo' />", theEmailImage.ContentId) + "</td><td> " + l.CategoryDesc + "</td></tr>"


   Next


  MailText = MailText.Replace("[Rows]", textBody)

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,396 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,821 Reputation points Microsoft Vendor
    2023-05-04T03:04:15.7033333+00:00

    Hi @peter liles,

    You just write Byte() data to MemoryStream.

            Dim ms As MemoryStream = New MemoryStream(myImage)
            Dim Img As LinkedResource = New LinkedResource(ms, MediaTypeNames.Image.Jpeg)
    

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. peter liles 556 Reputation points
    2023-05-04T18:11:04.21+00:00

    I don't fully understand how it works but it did :)

    the last part is confusing as i never seen that syntax code in vb.net before

    imageFiles.ForEach(Sub(img) alternateView.LinkedResources.Add(img)) ?

    This is a superb method because it leaves a blank space if no image present.

    Though that would not be ideal in practice. Therefore how is it possible to include a default image or a message to inform a recipient - no image available?

    Thanks


  2. peter liles 556 Reputation points
    2023-05-05T00:35:18.5433333+00:00

    yes. i just added a image from disk to the resource and that did the trick.

    it is a tricky task i am pleased for microsoft help otherwise i could not have solved this because i could not find any examples of this?

    saying that i dont know whether to curse microsoft for getting me started in the first place or see them as a good friend?!

    0 comments No comments