Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Saving of message/rfc822

843834Jul 5 2010 — edited Jul 5 2010
Hi there java expects

I am relatively new to Java, I have a javamail utility that gets mail from an exchange mail server and sends it to the users front end system it retrieves attachment and the mail body. The attachments are saved in a BLOB column, what I would like to achieve is to save the mimetype message/rfc822 to a blob column as well with an eml extension.

How can I do this, please help. Here is the attachment part of my code.... The code s very long

DROP JAVA SOURCE CQ.RECEIVEMAIL;

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED CQ.RECEIVEMAIL as import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import sqlj.runtime.*;
import oracle.sql.BLOB;

public class ReceiveMail
{
static void getAttachments(Message message, int incidentNo)
throws MessagingException, IOException, SQLException {
//String attachments = "";
Object content = message.getContent();
if (content instanceof Multipart)
{
// -- Multi part message which may contain attachment
Multipart multipart = (Multipart)message.getContent();
// -- Loop through all parts of the message
for (int i=0, n=multipart.getCount(); i<n; i++) {
Part part = multipart.getBodyPart(i);
String disposition = part.getDisposition();
if (Part.ATTACHMENT.equals(disposition)) {
//--if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
//-- This part is a file attachment
// --String fileName = incidentNo+"_"+part.getFileName().replace(' ','_');
String fileName = part.getFileName().replaceAll(" ","");
System.out.println("FILE: " + fileName);
String contentType = part.getContentType();
String mimeType = contentType.substring(0,contentType.indexOf(";"));
System.out.println("FILETYPE: " + mimeType);
InputStream is = part.getInputStream();
// -- To work with a BLOB column you have to insert a record
// -- with an emptly BLOB first.
#sql { insert into cq_incoming_attachments(att_seq, att_in_seq, att_file, att_attachment)
values (:incidentNo, :incidentNo||'_'||:i, :fileName, empty_blob()) };
// -- Retrieve the BLOB
BLOB attachment = null;
#sql { select att_attachment
into :attachment
from cq_incoming_attachments
where att_file = :fileName
and att_seq = :incidentNo
and att_in_seq = :incidentNo||'_'||:i };
// -- Fill the BLOB
OutputStream os = attachment.getBinaryOutputStream();
int j;
while ((j = is.read()) != -1) {
os.write(j);
}
is.close();
os.close();
// -- Set the BLOB by updating the record
#sql { update cq_incoming_attachments
set att_attachment = :attachment
where att_file = :fileName
and att_seq = :incidentNo };
#sql { update mail_inbox set attachment = 'Y', att_name = trim(att_name||' '||:fileName)
where att_seq = :incidentNo };
}
}
}
}
/
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 2 2010
Added on Jul 5 2010
1 comment
425 views