Invio di un messaggio con allegato il documento e i documenti collegati zippati (BPM)

' Il seguente frammento di codice invia il file del documento e quelli dei documenti collegati in unico zip a destinatari appartenenti ad uno specifico ente.

' Va a cercare i destinatari con ente  specifico, in esempio MD
Dim par3 As AssocArray = New AssocArray
q3=OpenTable(DB,"SELECT mail FROM PERSON WHERE mail is not null and (ente='MD' or ente2='MD' or ente3='MD' or ente4='MD' or ente5='MD')",par3,false,false,msg3)

if empty(msg3) and q3.rowset.first()

	'Se ci sono destinatari crea il messaggio
	Dim qmail as QWMail = New QWMail
	qmail.MsgCreate()
	'Inserire il mittente
	qmail.MsgFrom("[email protected]")
    do while not q3.rowset.endofset()
	'Vengono aggiunti come destinatari (TO)
      qmail.MsgAddDest(q3.rowset.fields(1).value, 1)  
	  q3.rowset.next()
	loop  
	'Vengono definiti SOGGETTO e TESTO del messaggio
	Subj = "Invio Documentazione - " + formfields("TIPO_CONT").Value + "/" + formfields("NUMERO").value
	Txt = "In allegato documento e, ove presenti, i file collegati raccolti in un archivio zip. "
	qmail.MsgSubject(Subj)
	qmail.MsgText(Txt)
    ' La query prende il percorso e il nome estraendolo da NOMEFILE, che contiene anche altri elementi
    Dim par As AssocArray = New AssocArray	  
	sql="select NOMEFILE, substring(NOMEFILE, charindex('_-',NOMEFILE)+2,len(cast(NOMEFILE as varchar(300)))) as TITOLO from linkdoc where iddoc1='DW' and :CODDOC0 like coddoc1+'%' and iddoc2='**' "
	par("CODDOC0")=CODDOC
    q=OpenTable(DB,sql,par,false,false,msg)  
	if msg = ""
	 if q.rowset.first() then
 	  files = New DBArray
	  filenames = New DBArray
         ' Crea un ciclo che va a prendere tutti i file associati e li mette nei vettori che saranno argomenti per la creazione  dello zip
		do while not q.rowset.endofset() 
         ' file collegati
			nome=q.rowset.fields("nomefile").value
       '  report della registrazione - se si volesse mettere nello zip
	   '  nome= QWSess.Q95_NOME_DIR_DOCUM + "\" + q.rowset.fields("DIRECTORY").value+"\ATTESA\"+q.rowset.fields("NOMEFILE").value
			If File (nome) Then
				files.Add(nome)
				filenames.Add(q.rowset.fields("TITOLO").value)
			End If
			q.rowset.Next()
		loop
		' Terminata la raccolta degli allegati, si genera lo zip e lo si allega
		zipname = funique(Q95_PATH_TEMP + "\temp??????.zip")
		Dim zipper As qwlib.Zipper = New qwlib.Zipper
		zipper.GenerateZipFile(zipname, files, filenames)
		qmail.MsgAttachment("Allegati.zip", zipname)
	end if  
	closetable(q)
 end if
    Dim par2 As AssocArray = New AssocArray
 	sql2="select d.NOMEFILE,td.DIRECTORY, d.TITOLO, d.PUBBLICATO,d.tipo_cont, d.numero, d.custom1 FROM DOCUMENT d join TIPI_DOC td on d.TIPO=td.TIPO where d.OBSOLETO=0 and d._coddoc=:CODDOC " 
	par2("CODDOC")=CODDOC
	q2=OpenTable(DB,sql2,par2,false,false,msg2)  
		
	if msg2="" and q2.rowset.first()
		if  q2.rowset.fields("PUBBLICATO").value = true
			percorsoreport = QWSess.Q95_NOME_DIR_DOCUM + "\" + q2.rowset.fields("DIRECTORY").value+"\DEFINITI\"+q2.rowset.fields("NOMEFILE").value
		else
			percorsoreport = QWSess.Q95_NOME_DIR_DOCUM + "\" + q2.rowset.fields("DIRECTORY").value+"\ATTESA\"+q2.rowset.fields("NOMEFILE").value 
		end if
		' Nome del file del documento nell'allegato
		titolo = q2.rowset.fields("NOMEFILE").value
		' Per sicurezza non tocco direttamente il file, ma lo copio in cartella temporanea
		percorsocopia = Q95_PATH_TEMP + "\" + titolo
		system.IO.file.copy(percorsoreport,percorsocopia,true)
		qmail.MsgAttachment(titolo,percorsocopia)
		msg = ""
		ok = qmail.MsgSend(msg)
		if ok = false then
			WriteLog("Messaggio non inviato: " + msg)
			else
			Write_LogDoc(QWSess,"DW",CODDOC,2,"Messaggio con allegati zip e report inviato")
		end if
	end if
    CloseTable(q2)		
end if
CloseTable(q3)