How send Notes values as MIME mail via agent?

Hi,

I have been reading some of the posts on how to send an HTML or MIME email from Notes on this forum. But have not seen an answer to what I need to do even though I would think it would be something a lot of people would want to do at some point. Can someone provide any code to do the following:

Am using a LS agent to run down all the Notes documents in a view and want to build a MIME mail with some of the values from each doc - - including email address for the SendTo address. Need a MIME mail because want to set some of the fonts in the mail with HTML. The confusing part was how to add values from the doc fields to the mail - especially if the field is MULTIVALUE.

Could someone shed some light on if this is possible and maybe an example of how it would be coded?

Thanks very much,

Tim

Subject: How send Notes values as MIME mail via agent?

The reason you don’t find a specific example of introducing values from multivalued fields into your MIME output, is that reading multivalue fields is nowhere near as complicated as generating MIME. Do something like this:

Dim prices As Variant

prices = doc.Prices

Dim i As Integer

For i = 0 to ubound(prices)

etcetera prices(i)

Next

Where instead of etcetera, you’re converting things to HTML and adding them to your MIME part. One thing you have to watch out for, is cases where the data from your document might contain characters that are syntax markers for HTML. You might want to use a subroutine such as the following to make strings into their HTML representation:

Function HTMLSafe(Byval strIn As String) As String

Static initialized As Boolean

Static fromArr(0 To 3) As String

Static toArr(0 To 3) As String

Dim target(0) As String, result As Variant



If Not initialized Then

	fromArr(0) = {&} 

	toArr(0) = {&}

	fromArr(1) = {<} 

	toArr(1) = {&lt;}

	fromArr(2) = {>} 

	toArr(2) = {&gt;}

	fromArr(3) = {"} 

	toArr(3) = {&quot;}

End If

target(0) = strIn

result = Replace(target, fromArr, toArr)

HTMLSafe = result(0)

End Function

This doesn’t really handle everything; you might want to add code to look at each character and convert it to a &number; if it’s within the non-ASCII range, but that also depends on your character set selection in the HTML block.

Subject: RE: How send Notes values as MIME mail via agent?

Andre,

Thanks very much for your response. No doubt it will come in handy.

But that is not the point I was confused about. This is probably elementary; too, but what I am not understanding is how to add fields from Notes docs to the “body” of the MIME mail. Read some references to the the ‘Parent’ and ‘child’ entities. Is this what I am suppose to use. If so, could you show any code examples of how this is done - especially when adding multivalue data to the body of the MIME mail.

Thanks again,

Tim

Subject: RE: How send Notes values as MIME mail via agent?

I’m not sure what you mean by ‘add fields from Notes docs to the “body” of the MIME mail’. The reason I’m not sure is that I told you how to do that, and you don’t seem to be satisfied. I assumed you meant, if the field contains the name “Sam”, you want “Sam” to appear as part of your message text. If not, please explain what you’re talking about.

If you want to insert text from the fields into the HTML of your message body, you just do that. You figure out what you want the HTML to be, putting the HTML together using text you have read from the fields. For instance, the lines in red show where we are getting fields from a document, and then inserting them into a string which later gets put into the contents of the HTML MIME part.

Dim session As New notessession

Dim wksp As New NotesUIWorkspace

Dim db As NotesDatabase

Dim docMemo As NotesDocument

Dim hdr As NotesMimeHeader

Dim stream As NotesStream

Dim actions As Variant	' contains an array of values from multivalue "actions" field.

Dim reasons As Variant	' contains an array of values from multivalue "reasons" field. We expect there are the same number of reasons and actions



Dim docCur As NotesDocument

Set docCur = wksp.CurrentDocument.Document



actions = docCur.GetItemValue("Actions")

reasons = docCur.GetItemValue("Reasons")



Set db = New NotesDatabase("", "")

db.OpenMail

Set docMemo = db.CreateDocument

docMemo.Subject = "Sample MIME message"

Set result = docMemo.CreateMIMEEntity("Body")

Set hdr = result.CreateHeader("MIME-Version")

Call hdr.SetHeaderValAndParams(|1.0|)

Set stream = session.CreateStream

Call stream.WriteText(|Some Text That Appears Before a Table.|)

If Ubound(actions) > 0 Or actions(0) <> "" Then

	Call stream.WriteText(|<table border=1 width=700><tr valign=top><td width=200><font size=2 face="sans-serif"><b>Action</b></font>
Reason|)
	Dim i As Integer

	For i = 0 To Ubound(actions)

		Call stream.WriteText(|<tr valign=top><td>| + HTMLSafe(actions(i)) + |</td><td>| + HTMLSafe(reasons(i)) + |</td></tr>|)

	Next

	Call stream.WriteText(|</table>|)

Else

	Call stream.WriteText(|<b>No data!</b>|)

End If

stream.Position = 0&

result.SetContentFromText stream, "text/html", ENC_NONE

Call docMemo.CloseMIMEEntities(True, "Result")	' finalize pending changes into the document.

Call docMemo.Send(False, "somebody@somewhere.org")

I suppose, if you wanted to do this with data binding instead, you could create one additional MIME part that contains your table of data as text/text, then add an OBJECT tag to your HTML that refers to it with a CID: URL. That seems like overkill for most applications, and I’m not sure the Notes client could figure out what to do with such an email if it received it. To see how to create multipart MIME, use the sample application I gave you a link to, and create a MIME Code Generator document, then put in some rich text that includes an image and generate the code for that.

Subject: RE: How send Notes values as MIME mail via agent?

Andre,

Thanks for all your help. Sorry if I was a bit dense on this. Believe I was trying to make it a lot harder than it needs to be (which is what I was afraid I was doing). Your example showed me exactly what I was looking for.

Sincerely,

Tim