|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Receiving mail with attachments and saving in Database |
« View previous topic :: View next topic » |
Author |
Message
|
Shuyin |
Posted: Mon Jul 30, 2012 1:42 pm Post subject: Receiving mail with attachments and saving in Database |
|
|
Novice
Joined: 26 Jul 2012 Posts: 23
|
Hi everybody! could you help me with the next issue please:
I'm receiving emails with an EmailInput node or a JavaCompute Node, the attachments in the mails are saved as BLOB in an Oracle DB.
When I extract the file (as BLOB) from the DB and I saved it in the filesystem the file are corrupted. For example, a receive an Excel2010 file and I saved it in the DB, after that I extract the BLOB and with a FileOutput Node I put the file into a Folder; when i open the Excel2010 file, Excel shows me that the file is damaged and ask to me if i want to repair it, at the end the file is repaired.
If i open the Excel2010 file with Notepad++, the file has NULL caracters at the end of file.
How could i solve this problem? |
|
Back to top |
|
 |
marko.pitkanen |
Posted: Mon Jul 30, 2012 9:58 pm Post subject: |
|
|
 Chevalier
Joined: 23 Jul 2008 Posts: 440 Location: Jamsa, Finland
|
Hi Shuyin,
What is your broker version and in which OS it runs?
Can you describe with more details (code samples etc.) how you are now doing your DB storing and fetching and creating a message for the FileOutput -node?
--
Marko |
|
Back to top |
|
 |
Shuyin |
Posted: Tue Jul 31, 2012 8:52 am Post subject: |
|
|
Novice
Joined: 26 Jul 2012 Posts: 23
|
marko.pitkanen wrote: |
Hi Shuyin,
What is your broker version and in which OS it runs?
Can you describe with more details (code samples etc.) how you are now doing your DB storing and fetching and creating a message for the FileOutput -node?
--
Marko |
Hi Marko,
Thanks for your help. My broker's version is 7.0.0.3. I'm working on Windows 7 Ultimate. About my flow, it has the following structure:
<TimeOutNotification>--><JavaCompute>--><Compute>--><FileOutput>
The timeoutnotification starts each 4 seconds,
My java compute has the following code:
Code: |
import com.ibm.broker.javacompute.Base64;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.*;
import com.sun.mail.util.QDecoderStream;
import com.sun.mail.util.QPDecoderStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Part;
import javax.mail.util.SharedByteArrayInputStream;
import sun.misc.BASE64Encoder;
public class Email_Recibo_RecepcionEmail extends MbJavaComputeNode {
private <T> T getContentType (Object o){
return (T)o;
}
public void evaluate(MbMessageAssembly contact admin) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage inMessage = contact admin.getMessage();
// create new message
MbMessage outMessage = new MbMessage();
MbMessageAssembly outAssembly = new MbMessageAssembly(contact admin,
outMessage);
try {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
// optionally copy message headers
copyMessageHeaders(inMessage, outMessage);
// ----------------------------------------------------------
// Add user code below
String host = "server ip";
String hostType = "pop3";
String user = "user";
String pass = "pass";
String from = "";
String to = "";
String subject="";
String sentdate="";
String mensaje="";
byte attachment[] = null;
Properties props = System.getProperties();
//prop.put("mail.pop3.host", host);
Session correoSession = Session.getDefaultInstance(props,null);
Store correoStore = correoSession.getStore(hostType);
correoStore.connect(host,user, pass);
Folder carpeta = correoStore.getFolder("INBOX");
carpeta.open(Folder.READ_WRITE);
Message correo[] = carpeta.getMessages();
for (int i = 0;i<1;i++)
{
from = correo[i].getFrom()[0].toString();
subject = correo[i].getSubject();
sentdate=correo[i].getSentDate().toString();
to = correo[i].getRecipients(Message.RecipientType.TO)[0].toString();
MbElement OutRoot = outMessage.getRootElement();
//MbElement OutBLOB = OutputRoot.createElementAsLastChildFromBitstream(attachment,MbBLOB.PARSER_NAME,null,null,null,0,0,0);
MbElement OutXML = OutRoot.createElementAsLastChild("XMLNSC");
MbElement OutCorreo = OutXML.createElementAsLastChild(MbXMLNSC.FOLDER, "CORREO", null);
OutCorreo.createElementAsFirstChild(MbXMLNSC.FOLDER, "FROM", from);
OutCorreo.createElementAsLastChild(MbXMLNSC.FOLDER, "TO", to);
OutCorreo.createElementAsLastChild(MbXMLNSC.FOLDER, "SUBJECT", subject);
OutCorreo.createElementAsFirstChild(MbXMLNSC.FOLDER, "DATE", sentdate);
if(correo[i].isMimeType("text/*")){
try{
//Object content = correo[i].getContent();
//InputStream mensaje2 = null;
//mensaje2 = getContentType(content);
//byte[] mensajeB = new byte[mensaje2.available()];
//mensaje2.read(mensajeB);
//mensaje = new String(mensajeB);
mensaje = correo[i].getContent().toString();
System.out.println(mensaje);
OutCorreo.createElementAsFirstChild(MbXMLNSC.FOLDER, "MENSAJE", mensaje);
}catch(Exception e){}
}
if(correo[i].isMimeType("multipart/*")){
Multipart multi;
multi= (Multipart)correo[i].getContent();
for(int j = 0; j<multi.getCount();j++){
Part parte = multi.getBodyPart(j);
if(parte.isMimeType("text/plain")){
try{
//Object content = parte.getContent();
//InputStream mensaje2 = null;
//mensaje2 = getContentType(content);
//byte[] mensajeB = new byte[mensaje2.available()];
//mensaje2.read(mensajeB);
//mensaje = new String(mensajeB);
mensaje = parte.getContent().toString();
System.out.println(mensaje);
OutCorreo.createElementAsFirstChild(MbXMLNSC.FOLDER, "MENSAJE", mensaje);
}catch(Exception e)
{e.printStackTrace();}
}else
if(parte.isMimeType("multipart/*")){
Multipart multi2 = (Multipart)parte.getContent();
for(int k = 0; k<multi2.getCount();k++)
{
Part parte2 = multi2.getBodyPart(k);
if(parte2.isMimeType("text/plain"))
{
mensaje = parte2.getContent().toString();
System.out.println(mensaje);
OutCorreo.createElementAsFirstChild(MbXMLNSC.FOLDER, "MENSAJE", mensaje);
}else
if(!parte2.isMimeType("text/html"))
{
Object content = parte.getContent();
InputStream mensaje2 = null;
mensaje2 = getContentType(content);
attachment = new byte[mensaje2.available()];
mensaje2.read(attachment);
int tam = attachment.length;
MbElement OutAdjunto = OutXML.createElementAsFirstChild(MbXMLNSC.FOLDER, "ADJUNTO", null);
OutAdjunto.createElementAsFirstChild(MbXMLNSC.FOLDER, "NOMBRE", parte.getFileName());
OutAdjunto.createElementAsLastChildFromBitstream(attachment,MbBLOB.PARSER_NAME,null,null,null,0,0,0);
}
}
}else
if(!parte.isMimeType("text/hmtl"))
{
Object content = parte.getContent();
InputStream mensaje2 = null;
if(parte.isMimeType("text/*")){
String m = content.toString();
attachment = new byte[m.length()];
attachment = m.getBytes();
}else{
mensaje2 = getContentType(content);
//mensaje2 = parte.getInputStream();
attachment = new byte[mensaje2.available()];
mensaje2.read(attachment);
}
int tam = attachment.length;
System.out.println(tam);
MbElement OutAdjunto = OutXML.createElementAsFirstChild(MbXMLNSC.FOLDER, "ADJUNTO", null);
OutAdjunto.createElementAsFirstChild(MbXMLNSC.FOLDER, "NOMBRE", parte.getFileName());
OutAdjunto.createElementAsLastChildFromBitstream(attachment,MbBLOB.PARSER_NAME,null,null,null,0,0,0);
}
}
}
correo[i].setFlag(Flags.Flag.DELETED, true);
}
// End of user code
// ----------------------------------------------------------
// The following should only be changed
// if not propagating message to the 'out' terminal
carpeta.close(true);
correoStore.close();
out.propagate(outAssembly);
outMessage.clearMessage();
}
catch(MessagingException me)
{
String Error = me.getMessage();
System.out.println(Error);
}
catch(MbException mb){}
catch(IOException ioe){}
catch(Exception e)
{
String Error = e.getMessage();
e.printStackTrace();
}
finally {
// clear the outMessage even if there's an exception
outMessage.clearMessage();
}
}
|
*When i see the values of "tam", it has a few bytes more than the original file.
And my compute node is as follow:
Code: |
DECLARE metadata,remitente,destinatario,asunto,mensaje,mensaje2,nombre,ext,ctype,attchar CHAR '';
DECLARE adjunto BLOB;
DECLARE fecha_envio,fecha_recep DATE;
DECLARE tipo,idestadocorreo,maximo,maximo_arc,num_adj,cont_adj,tam INTEGER 0 ;
--CALL CopyMessageHeaders();
CALL CopyEntireMessage();
SET remitente = InputRoot.XMLNSC.CORREO.FROM;
SET destinatario = InputRoot.XMLNSC.CORREO.TO;
SET asunto = InputRoot.XMLNSC.CORREO.SUBJECT;
--SET fecha = CAST(InputRoot.XMLNSC.CORREO.DATE AS DATE FORMAT 'DD/MM/YY');
SET fecha_envio = CURRENT_DATE;
SET fecha_recep = CURRENT_DATE;
SET mensaje = InputRoot.XMLNSC.CORREO.MENSAJE;
SET tipo = 0;
SET metadata = null;
SET idestadocorreo = 1;
SET Environment.max[] = PASSTHRU ('SELECT SEQ_CORREO.nextVal FROM dual');
SET maximo = CAST(Environment.max[1].NEXTVAL AS INTEGER);
INSERT INTO Database.CORREO VALUES(maximo,remitente,destinatario,asunto,fecha_envio,mensaje,tipo,metadata,idestadocorreo,'',fecha_recep);
SET num_adj = CARDINALITY(InputRoot.XMLNSC.ADJUNTO[]);
SET cont_adj = 1;
ADJ:WHILE (cont_adj<=num_adj) DO
--SET ctype = CAST(InputRoot.MIME.Parts.Part[cont_adj]."Content-Type" AS CHAR);
--SET nombre = SUBSTRING(ctype AFTER '"');
--SET nombre = SUBSTRING(nombre BEFORE '"');
SET nombre = InputRoot.XMLNSC.ADJUNTO[cont_adj].NOMBRE;
--IF (nombre = '') THEN
-- SET nombre = SUBSTRING(ctype AFTER 'name=');
--END IF;
IF (CONTAINS(nombre,'.')) THEN
SET ext = SUBSTRING(nombre AFTER '.');
ELSE
SET ext = '';
END IF;
--SET attchar = CAST(InputRoot.MIME.Parts.Part[cont_adj].Data.BLOB.BLOB AS CHAR CCSID InputProperties.CodedCharSetId);
--SET adjunto = BASE64DECODE(attchar);
--SET adjunto = BASE64DECODE(BASE64ENCODE(InputRoot.MIME.Parts.Part[cont_adj].Data.BLOB.BLOB));
SET adjunto = InputRoot.XMLNSC.ADJUNTO[cont_adj].BLOB.BLOB;
--SET adjunto = TRIM(TRAILING X'0d0a' FROM InputRoot.MIME.Parts.Part[cont_adj].Data.BLOB.BLOB);
--SET adjunto = TRIM(TRAILING X'00' FROM InputRoot.MIME.Parts.Part[cont_adj].Data.BLOB.BLOB);
--SET adjunto = TRIM(TRAILING X'00' FROM adjunto);
--SET tam = LENGTH(BASE64DECODE(CAST(InputRoot.MIME.Parts.Part[cont_adj].Data.BLOB.BLOB AS CHARACTER)));
--SET tam = CAST(InputRoot.MIME.Parts.Part[cont_adj]."Content-Transfer-Encoding" AS CHAR);
SET Environment.max_arc[] = PASSTHRU ('SELECT SEQ_ARCHIVO_ADJUNTO.nextVal FROM dual');
SET maximo_arc = CAST(Environment.max_arc[1].NEXTVAL AS INTEGER);
INSERT INTO Database.ARCHIVO_ADJUNTO VALUES (maximo_arc, adjunto);
INSERT INTO Database.ARCHIVO_ADJUNTO_INFO VALUES (maximo_arc,nombre,1,ext);
--COMMIT;
SET cont_adj = cont_adj+1;
SET OutputRoot.BLOB.BLOB = adjunto;
SET OutputLocalEnvironment.Destination.File.Name = nombre;
END WHILE ADJ;
--RETURN TRUE;
|
*if i compare the new file (fetched from the mail) and the original file, the new one has NULL characters at the end of file.
*I use Notepad++ for compare, and it shows me than the files are equals. |
|
Back to top |
|
 |
marko.pitkanen |
Posted: Tue Jul 31, 2012 1:05 pm Post subject: |
|
|
 Chevalier
Joined: 23 Jul 2008 Posts: 440 Location: Jamsa, Finland
|
Hi Shuyin,
I think that you should research next what they says about using InputStream. Hopefully someone more skilled Java developer than I looks up your code too.
Another thing I wonder is why you are copying whole message in your ESQL and then assigning adjunto in loop to BLOB parser in addition to the copied XMLNSC?
--
Marko |
|
Back to top |
|
 |
marko.pitkanen |
Posted: Wed Aug 01, 2012 1:13 am Post subject: |
|
|
 Chevalier
Joined: 23 Jul 2008 Posts: 440 Location: Jamsa, Finland
|
|
Back to top |
|
 |
marko.pitkanen |
Posted: Wed Aug 01, 2012 1:28 am Post subject: |
|
|
 Chevalier
Joined: 23 Jul 2008 Posts: 440 Location: Jamsa, Finland
|
|
Back to top |
|
 |
Shuyin |
Posted: Thu Aug 02, 2012 11:01 am Post subject: Problem solved. |
|
|
Novice
Joined: 26 Jul 2012 Posts: 23
|
Hi Marko!!!!
Thanks so much for your help!. I've tried use the EmailInput Node, but this node has the same problem. For solve the issue i've modified the Java code in the part where i declared my byte array, its size was bigger than the file size.
Thanks again and have a good day!  |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|