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
S status=Message.AttachFile(Dir,FileName)
D 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
Comments
See my old article on my blog: Examples of generating and sending Email using the Caché DBMS (this is machine translation)
Hi,
Thank you, the link does not open for me.
Can you post the code example?
In this case you can open the original article and take the code from there (and/or translate it with another service, for example Google translate)
Thank you!
I will look into it.
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.Write($system.Encryption.Base64Decode("iV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Rgfsh95G/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)
I got it to work,
Turns out that angle brackets must be added in the Content-ID header for this to work in Gmail.
do part.Headers.SetAt("<id1>","Content-ID")
It's mentioned in this article , remark by masoudmanson.
https://stackoverflow.com/questions/41946783/gmail-blocking-small-embed…
Thanks for helping me solve this problem! I appreciate it very much.