Embedded images as linked resource in Gmail is being sent as an attachment

Hello folks, I’m trying to send an email to be opened in gmail web app, and saw this reference (among many references here): Send Embedded Images in an Email

The first two approaches didn’t work, but the third one, while more technical and complicated, worked. The problem is it worked only when embedding a single image. I need to attach several images, but when I do it via this approach, it sends the images as attachments instead. What seems to be causing this issue?

@earldantedumayag

Can you try as below


Imports System.IO
Imports System.Net.Mail
Imports System.Net.Mime

' Define the email content IDs and image paths
Dim contentId1 As String = "image1"
Dim contentId2 As String = "image2"
Dim imagePath1 As String = "C:\images\image1.png"
Dim imagePath2 As String = "C:\images\image2.jpg"

' Create a new email message
Dim message As New MailMessage()
message.From = New MailAddress("sender@yourdomain.com")
message.To.Add("recipient@theirdomain.com")
message.Subject = "My email with inline images"

' Define the HTML email content with image sources set to cid:content-id
Dim html As String = "
<html>
  <body>
    <p>Here are my images:</p>
    <img src='cid:" & contentId1 & "'>
    <img src='cid:" & contentId2 & "'>
  </body>
</html>
"
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(html, Nothing, MediaTypeNames.Text.Html)
message.AlternateViews.Add(htmlView)

' Load the images from disk and attach them to the email message
Dim image1 As LinkedResource = New LinkedResource(imagePath1, MediaTypeNames.Image.Png)
image1.ContentId = contentId1
htmlView.LinkedResources.Add(image1)

Dim image2 As LinkedResource = New LinkedResource(imagePath2, MediaTypeNames.Image.Jpeg)
image2.ContentId = contentId2
htmlView.LinkedResources.Add(image2)

' Connect to the SMTP server and send the email
Dim client As New SmtpClient("smtp.yourdomain.com")
client.Send(message)

Hope this may help you

Thanks,
Srini

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.