Question
· Oct 26, 2021

Sending Mail with embedded image using %Net.SMTP

Hi,

I wonder if anyone tackled the following problem-

Sending a mail with an embedded image is straight forward using %Net.SMTP

status=Message.AttachFile(Dir,FileName)

Message.TextData.Write("..<img src=""cid:xyz.png"">..")

Outlook displays such an Email as expected- with the image in the body of the mail.

Gmail displays the image as an attached file.

There are a lot of suggestions online about this, but none is working for me.

Did anyone face and solved this issue?

Regards,

Nael Naseraldeen

Product version: Caché 2016.1
$ZV: Cache for Windows (x86-64) 2016.2.1 (Build 803_0_17317) Mon May 8 2017 15:05:01 EDT
Discussion (6)0
Log in or sign up to continue

Hi,

I took the code from the article, still the same problem.. in Outlook I get a nice embedded pic,

In Gmail- the pic is attached.   

It seems there is a specific problem with Gmail displaying embedded images.

Did you manage to get a mail with embedded image to display correctly in Gmail?

I also tried the base64 method, and that also worked correctly only when the mail was opened in Outlook.

Method 1:

Set tmpEmail=##class(%Net.SMTP).%New()
  Set tmpEmail.smtpserver="xyz"
  Set msg=##class(%Net.MailMessage).%New()
  Set msg.From="from.."
  Set msg.Charset="Windows-1255"
  D msg.To.Insert("to..")
  Set msg.Subject="testing"
  set msg.IsHTML=1   
  set msg.MultiPartType="related"   
  do msg.AttachFile("c:\xyzxyz\","a.png")
  set part=msg.Parts.GetAt(1)
  set part.ContentType="image/png"
  set part.InlineAttachment=1 // otherwise, the default is attachment. graphic inside html
  do part.Headers.SetAt("id1","Content-ID")   
  do msg.TextData.Write("<b>Hello< / b>, </b><font color='red'>World<font color='red'></font>!")
  do msg.TextData.Write("<br/><img src='cid:id1'>")
  s status= tmpEmail.Send(msg)
 

Method 2 base64

  Set tmpEmail=##class(%Net.SMTP).%New()
  Set tmpEmail.smtpserver="xyz"
  Set msg=##class(%Net.MailMessage).%New()
  Set msg.From="from.."
  Set msg.Charset="Windows-1255"
  D msg.To.Insert("to..")
  Set msg.Subject="testing base64"
  set msg.IsHTML=1   
  set msg.MultiPartType="related"
  set stream=##class(%GlobalBinaryStream).%New()
  do stream.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WAhHCC1MNK/+lWA0cJLRgfsh95G/N1H136z7+BCSuHMuMEGVLo4a1XCKKcuO0WXZBw1FcNighmPWbe8jIeeJpdFiCrkHguWUyM92NBKseBut8T92bfsw66z49IyrHH2T5CvlqeCYUvL/ANIZ7+ohXSdWAAAAAElFTkSuQmCC"))
  do msg.AttachStream(stream,"id1")
    
  set part=msg.Parts.GetAt(1)
  set part.ContentType="image/png"
  set part.InlineAttachment=1    // otherwise, the default is attachment. graphic inside html
  do part.Headers.SetAt("id1","Content-ID")

  do msg.TextData.Write("<b>Hello< / b>, </b><font color='red'>World<font color='red'></font>!")
  do msg.TextData.Write("<br/><img src='cid:id1'>")
  s status= tmpEmail.Send(msg)