Calling all MIME / Rich Text Message experts

Hi,

I have a requirement to send notes rich text emails with HTML style links and inline images.

No file attachments should show within the mail body, yet the images need to be visible when the user is offline.

After searching for similar topics both here and in google, it seemed a good way to tackle this might be to use the NotesMIMEEntity class together with a NotesStream to do so.

I got the html part working fine, but I’m having real problems getting images to appear.

FYI the emails are being created on the fly by a scheduled LS agent, and I can’t make mods to the mail template. My code is below. If anyone could offer me some advice I’d be really grateful. I’m afraid I haven’t been able to find many examples of inline MIME images on the web…

Sub Initialize

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument, formatDoc As NotesDocument

Dim body As NotesMIMEEntity, mimeImage As NotesMIMEEntity, mimeText As NotesMimeEntity

Dim header As NotesMimeHeader, mimeHeader As NotesMIMEHeader

Dim stream As NotesStream

Set db = s.CurrentDatabase

s.ConvertMIME = False ' Do not convert MIME to rich text

Set doc = db.CreateDocument

doc.Form = "Memo"

doc.Subject = "Newsletter MIME Test"

doc.Principal = "Newsletter Team"

Set body = doc.CreateMIMEEntity

’ Create main MIME message

Set stream = s.CreateStream		

Set header = body.CreateHeader("MIME-Version")

Call header.SetHeaderVal("1.0")	

Set header = body.CreateHeader("Content-Type")

Call header.SetHeaderValAndParams(|multipart/related;boundary="*_bounds_*"|) ' Parts are components of a single document (for example, an HTML page and the images in that page).

’ Create Message text

Set mimeText = body.CreateChildEntity	

Call stream.WriteText(|<style>TD {font-family:arial;font-size:9pt;}</style><table width="100%" border="0" cellspacing="0">| &_

|<tr><td>Image Link</td><td>|)

Call stream.WriteText(|<a href="http://www.lotus.com">Lotus Website</a>| &_

|<img src="cid:-*msg.LotusImage*-" alt="Lotus" width="240" height="30"></td></tr></table>|)

Call mimeText.SetContentFromText(stream,"text/html;charset=iso-8859-1",ENC_QUOTED_PRINTABLE)	

’ Create inline image reference

Set mimeImage = body.CreateChildEntity

Set mimeHeader = mimeImage.CreateHeader("Content-ID")

Call mimeHeader.SetHeaderVal("-*msg.LotusImage*-")

Set mimeHeader = mimeImage.CreateHeader("Content-Disposition")

Call mimeHeader.SetHeaderValAndParams("inline;filename=c:\documents and settings\ghinton\desktop\km newsletters\images\db_monitor_industries.gif")

Call mimeImage.SetContentFromBytes(stream, "image/gif",ENC_IDENTITY_BINARY)	

’ Call body.SetContentFromText(stream, “text/html”, ENC_NONE) ’ think this is what I’m getting wrong??

’ Call stream.Truncate ’ deletes the stream object from memory

Call doc.Send(False, "Giles Hinton")

s.ConvertMIME = True ' Restore conversion

End Sub

Subject: Calling all MIME / Rich Text Message experts

If you get the answer, I’d like to see it as well, as I couldn’t get this working properly myself. While we have a product that does this, I try to always understand the capabilities of the core product, so that I don’t wind up pushing a product solution without clearly stating that there is a native solution.

Now, if you can’t get it working, even though I think you will, a fallback solution is to try our Midas Rich Text LSX, which will let you send MIME e-mails easily and with little coding. Just look at the Send It! sample on our website.

Subject: * I’m also interested in the answer…

Subject: Calling all MIME / Rich Text Message experts

Dim ses As New NotesSession	ses.ConvertMIME = False



Dim db As NotesDatabase	

Set db = ses.CurrentDatabase



Dim doc As NotesDocument

Set doc = db.CreateDocument

doc.Form = "Memo"

doc.Subject = "Newsletter MIME Test"

doc.Principal = "Newsletter Team"



Dim body As NotesMIMEEntity

Dim mc As NotesMIMEEntity

Dim mh As NotesMimeHeader

Dim stream As NotesStream	

Set body = doc.CreateMIMEEntity

’ Create main MIME message

Set mh = body.CreateHeader({MIME-Version})

mh.SetHeaderVal {1.0}



Set mh = body.CreateHeader({Content-Type})

mh.SetHeaderValAndParams {multipart/related;boundary="=NextPart_="}

’ HTML Entitity

Set mc = body.CreateChildEntity



Set stream = ses.CreateStream



stream.WriteText |<style>TD {font-family:arial;font-size:9pt;}</style><table width="100%" border="0" cellspacing="0">|

stream.WriteText |<tr><td>Image Link</td><td>|

stream.WriteText |<a href="http://www.lotus.com">Lotus Website</a>|

stream.WriteText |<img src="cid:-*msg.LotusImage*-" alt="Lotus" width="240" height="30"></td></tr></table>|



mc.SetContentFromText stream, {text/html;charset="iso-8859-1"}, ENC_QUOTED_PRINTABLE

stream.Close

’ Inline Image Entity

Set mc = body.CreateChildEntity



Set mh = mc.CreateHeader({Content-ID})

mh.SetHeaderVal {-*msg.LotusImage*-}



Set stream = ses.CreateStream

stream.Open {c:\image.gif}

mc.SetContentFromBytes stream, {image/gif;name="image.gif"}, ENC_IDENTITY_BINARY

stream.Close



doc.Send False, {Giles Hinton}

ses.ConvertMIME = True

Subject: RE: Calling all MIME / Rich Text Message experts

Thanks for posting that code Raymond.

Typically, I managed to find a fix just before you posted it, but I’d still like to thank you for taking the time to post the solution.

I’m interested as to whether some of the parameters and values are necessary when using notes - I previously added a load of them in a last ditch attempt to make it work! I ended up finally finding an example database in the sandbox (always the way - trawl forums forever but never think of looking in the most obvious place!).

My code is now working with a few less parameters:

Sub Initialize

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim mimeRoot As NotesMimeEntity, mime As NotesMimeEntity

Dim header As NotesMimeHeader

Dim stream As NotesStream

Set db = s.CurrentDatabase

s.ConvertMIME = False ' Do not convert MIME to rich text

Set doc = db.CreateDocument

doc.Form = "Memo"

doc.Subject = "MIME HTML inline test"

doc.Principal = "System"

’ Create main MIME message

Set stream = s.CreateStream		

Set MIMERoot = doc.CreateMIMEEntity ' message root

’ Create child entities

Set mime = MIMERoot.CreateChildEntity

Set header = MIMERoot.getNthHeader("Content-Type")

Call header.SetHeaderVal("multipart/related")	

’ Create Message text

Call stream.WriteText(|<style>TD {font-family:arial;font-size:9pt;}</style><table width="100%" border="0" cellspacing="0">| &_

|<tr><td><a href="http://www.lotus.com">Lotus Website</a>| &_	

||)

Call mime.SetContentFromText(stream,"text/html",ENC_NONE)		

Call stream.Close

’ Create inline image reference

Set mime = MIMERoot.CreateChildEntity	

Call stream.Open("c:\lotus_site.jpg")

Call mime.SetContentFromBytes(stream, "image/jpeg",ENC_NONE)

Call stream.Close

Call mime.EncodeContent(ENC_BASE64)

Set header = mime.CreateHeader("Content-ID")

Call header.SetHeaderVal("<lotus_site.jpg>")



Call doc.Send(False, "Giles Hinton")

s.ConvertMIME = True ' Restore conversion

End Sub

Thanks again though to Raymond - I’ll make sure I try and get your attention if I need more help!

Subject: Short blog on sending MIME HTML messages with Domino

Thought this might be of use to any other poor souls who need the answer to this question.

http://notesdev.blogspot.com/2004/07/sending-and-receiving-html-messages.html

Subject: RE: Short blog on sending MIME HTML messages with Domino

Thanks for all the details, saved me a lot of time.

As i have stored the informations in a Database and so the Images are not stored on the harddrive, i first have to extract them.

The solution below works fine, until c:\temp exists and enough free space …

perhaps it helps someone or any one has a solution for me how to get the input stream of the image directly from the document.

something else than

Call stream.Open( “c:\temp\image1.jpg”)

here my little bit of work …

joerg

Sub Initialize

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

’ The Original Message

Dim Oridoc As NotesDocument

Dim OriAttPath As String



Dim OriBody As String



Dim OriAttachment As String





Dim Orirtitem As Variant

Dim fileCount As Integer

fileCount = 0    

Set Oridoc = s.DocumentContext

OriAttachment = "." & Oridoc.GetItemValue("attachment_type")(0)

OriBody = Oridoc.GetItemValue("Body")(0)

Set Orirtitem = Oridoc.GetFirstItem( "img" )

If ( Orirtitem.Type = RICHTEXT ) Then

	Forall o In Orirtitem.EmbeddedObjects

		If ( o.Type = EMBED_ATTACHMENT ) Then

			fileCount = fileCount + 1

			Call o.ExtractFile _

			( "c:\temp\image" & Cstr(fileCount) & OriAttachment )

			

		End If

	End Forall

End If







Dim mimeRoot As NotesMimeEntity, mime As NotesMimeEntity

Dim header As NotesMimeHeader

Dim stream As NotesStream

Set db = s.CurrentDatabase

s.ConvertMIME = False ' Do not convert MIME to rich text

Set doc = db.CreateDocument

doc.Form = "Memo"

doc.Subject = Oridoc.Subject 

doc.Principal = "the Support team"

’ Create main MIME message

Set stream = s.CreateStream 

Set MIMERoot = doc.CreateMIMEEntity ' message root

’ Create child entities

Set mime = MIMERoot.CreateChildEntity

Set header = MIMERoot.getNthHeader("Content-Type")

Call header.SetHeaderVal("multipart/related") 

’ Create Message text

Call stream.WriteText(|<style>TD {font-family:arial;font-size:9pt;}</style><table width="100%" border="0" cellspacing="0">| &_

|<tr><td> | & |1.|  & | </td><td> | & Oribody & | </td><td> | & _

|<img src="cid:image1.jpg" alt="image" ></td></tr>| & _ 

|</table><div style=display:"none"> <font color="white"> | )

Call mime.SetContentFromText(stream,"text/html",ENC_NONE) 

Call stream.Close

’ Create inline image reference

Set mime = MIMERoot.CreateChildEntity 

Call stream.Open( "c:\temp\image1.jpg")



Call mime.SetContentFromBytes(stream, "image/jpeg",ENC_NONE)

Call stream.Close

Call mime.EncodeContent(ENC_BASE64)

Set header = mime.CreateHeader("Content-ID")

Call header.SetHeaderVal("<image1.jpg>")





Call doc.Send(False, "Joerg Velletti")

Call doc.Send

s.ConvertMIME = True ' Restore conversion

End Sub

Subject: RE: Short blog on sending MIME HTML messages with Domino

I was going to do this, but wasn’t able to use http to get the files, as it would have required a login to the database in order to retrieve the file (well, I thought it would, so I didn’t bother!)

Don’t see why you can’t use a standard http:// reference to an image resource or document attachment in the database though. Have you tried it?