0

I have followed the instructions for MailKit by Jeffrey Stedfast at MimeKit.net with further input from his helpful response to another Stack Overflow question to create an email with an embedded image. The result is that it saves space for the image but places the image at the end of the email as an attachment. Here is the code:

Dim clsSMTP = New MailKit.Net.Smtp.SmtpClient
clsSMTP.Connect("smtp.gmail.com", 587, False)
clsSMTP.Authenticate("xxxxxxxx@gmail.com", "xxxxxxxxxx")


strMessage = "<!DOCTYPE html><html><body style='font-family:Verdana,Arial,Tahoma,Calibri,Neue Helvetica;font-size:11pt;font-weight:normal;font-style:italic'>" _
& "<table><tr><td style='width:300px'><img src='cid:mptIntro.ContentId' style='width:300px'/></td><td>    </td>" _
    & "<td><h1 style='font-size:16pt;font-weight:bold;'>Time for an Advent Study</h1></td></tr></table>"

bdyBuild = New BodyBuilder
strPathIntro = "C:/Users/xxx/xxxxx/Images/xxxxxxxxxx.jpg"
mptIntro = bdyBuild.LinkedResources.Add(strPathIntro)
mptIntro.ContentId = MimeKit.Utils.MimeUtils.GenerateMessageId()
mptIntro.ContentDisposition = New MimeKit.ContentDisposition(MimeKit.ContentDisposition.Inline)

strMember = "Me"
strAttendeeEmail = "me@att.net"

Dim mmgSpecial As New MimeMessage()
mmgSpecial.From.Add(New MailboxAddress("xxx xxxxx xxxxx", "xxxxxxxxx@gmail.com"))
mmgSpecial.To.Add(New MailboxAddress(strMember, strAttendeeEmail))
mmgSpecial.Subject = "Study Ideas"
bdyBuild.HtmlBody = strMessage
mmgSpecial.Body = bdyBuild.ToMessageBody()

clsSMTP.Send(mmgSpecial)

Thank you for any help.

StuKH
  • 31
  • 7
  • Please [edit] the question to add a link to the "another Stack Overflow question". It may give us some useful context. – Andrew Morton Nov 29 '22 at 18:02
  • Shouldn't it be, e.g., `mptIntro.ContentId = MimeKit.Utils.MimeUtils.GenerateMessageId() [...] strMessage = $"... img src = 'cid:{mptIntro.ContentId}' ..."`? – Jimi Nov 29 '22 at 18:40
  • Andrew the Jeffrey Stedfast response in "another Stack Overflow question" is https://stackoverflow.com/questions/62159470/cannot-embed-images-in-email-with-mailkit – StuKH Nov 29 '22 at 19:42
  • Jimi - thanks for your input. I used the exact syntax that Jeffrey Stedfast used in the "another Stack Overflow question". I have no trouble getting the image. It shows up in the email but not embedded. Instead at the bottom of the message along with then anme of the image which was not wanted. – StuKH Nov 29 '22 at 19:45
  • I used the SmtpClient that is now deprecated and Using LinkedResources and AlternateViews I produced the email exactly as it should be. Unfortunately, the SmtpClient is not able to send the email in a loop. I can get it to send the first one from a list but then it errors out. I say this only to say that I know the email can be created in the manner I have listed above. – StuKH Nov 29 '22 at 19:52
  • 1
    Nope, it's not the same thing, you're using a string literal here: ``, not the value of `mptIntro.ContentId`, that's why in my comment you can see an interpolated string that sets the value of `mptIntro.ContentId`, after this value has been created -- In the answer that you have now linked, `string.Format()` is used in place of an interpolated string (which is quite exactly the same thing, just different syntax) – Jimi Nov 29 '22 at 20:01
  • Note that you can also embed images as base64 strings, with, e.g, `$" [...] [...]"` (assuming a JPEG image) -- Still using an interpolated (`$"...{}"`) string – Jimi Nov 29 '22 at 20:10
  • Jimi - Thanks. The string.Format() worked well after i fiddled with it for awhile – StuKH Nov 30 '22 at 06:41

0 Answers0