Reading an attachment without detaching it

Howdy:

I’m looking for confirmation that something can’t be done. I want to write an “after mail arrives” agent that looks at an e-mail for a TXT attachment, opens it, reads the data into a set of documents, then closes the document. The trick is, I have to do this without detaching the file. I know I can do this with a Java agent, and I’m certain that I can do this in API, but the client insists that the agent can’t use those languages unless we’re certain it can’t be done with lotusscript.

I’m using 6.5 designer, but the server is using 7.0 so if there’s some 7 code that will do the trick, I can use it.

Thanks much.

Subject: Reading an attachment without detaching it

Why not detach it and delete it when you are are done?

Subject: RE: Reading an attachment without detaching it

“Why not detach it and then delete it when you are done?”

Yeah, that’s what I’ve done in the past with great success.

This time, though, my client’s administrators don’t allow for any OS access to a server. Apparently they’ve been burned a couple of times by poorly written agents that filled up or corrupted a server. In addition, there’s something with how their SAN is billed.

Subject: RE: Reading an attachment without detaching it

Thats too bad, is there is no other network share available for this?

Using the UNC (and giving the domino server service acess to the share), you can still get around the admin groups OS security restrictions.

You also might want to take a look at notesstream class - have not used it but it might help with what you are trying to do.

Subject: RE: Reading an attachment without detaching it

One way may be to use the XML functions.

Use NotesDXLExporter to export a document to a NotesStream. Then read the NotesStream.

If the incoming email was MIME-based you could use the NotesMIMEEntity class.

Subject: RE: Reading an attachment without detaching it

"One way may be to use the XML functions.

Use NotesDXLExporter to export a document to a NotesStream. Then read the NotesStream."

That just might work. I’ll post the code if I can make it happen.

Subject: Reading an attachment without detaching it [It Worked!]

Thanks to Tom Kuczek and Andrew Chung.

The only complication is that because DXL uses Base64 encryption, you need to decode the FILEDATA with the Base64 Lotusscript library that you can find here:

http://www.nsftools.com/tips/NotesTips.htm

[The same page also has a really useful script library for parsing CSV files with XML and another one for just reading CSV. These were no good for me, but might be what you’re looking for]

Obviously the below code is a bit ugly and you–the person looking to use this code–are going to have to do things so that you take care of multiple attachments, attachments in a different format, work with the right kind of attachment, and you’ll probably want some error checking. But we can succesfully read an attachment without detaching it:

Sub Initialize

Dim s As New notessession, db As NotesDatabase, dc As NotesDocumentCollection, d As notesdocument

Dim Stream As NotesStream, export As NotesDXLExporter

Dim x As Integer	, st As String, Start As Double, Finish As Double, fd As String



Set db=s.CurrentDatabase

Set dc=db.UnprocessedDocuments

For x=1 To dc.count

	Set d=dc.GetNthDocument(x)

	If Ucase(d.form(0))="MEMO" Then 

'Initalize the stream and DXL Exporters

		Set stream=s.createstream

		Set export=s.CreateDXLExporter

'Convert the document to DXL

		Call export.setInput(d)

		Call export.setOutput(stream)

		Call export.process	

'Process the export into a string file (as long as its less than 2GB)

		st=stream.ReadText

'Find the tags and read what’s between them

		Start=Instr(1,st,"<filedata>")

		Finish=Instr(1,st,"</filedata>")

		fd=Mid(st,Start +10, Finish-Start-11)

'Decode from Base 64

		fd=decodeBase64(fd)

'fd is your resulting data. Do with it what you will.

	End If

Next

End Sub

Subject: RE: Reading an attachment without detaching it [It Worked!]

Huzzah!