Home » Developer & Programmer » JDeveloper, Java & XML » JavaMail inside Database (Oracle 10g)
icon9.gif  JavaMail inside Database [message #282381] Wed, 21 November 2007 14:46
Messages: 51
Registered: April 2007
Location: Azerbaijan

Hi everyone.

I'm using Oracle Database 10gR2, and mail server is MDaemon installed on my computer.

I've found the procedure in java which connects to the Mail server and downloads messages using JavaMail package.

Here it is. I've chenged it some.
package mail;

import javax.mail.*;

import java.util.*;

import java.io.*;

public class RM {
  private void handle(Message msg) throws Exception {
    System.out.println("Subject: " + msg.getSubject());
    System.out.println("From: " + msg.getFrom()[0].toString());
    System.out.println("Sending date: " + msg.getSentDate());

  private void handleText(Message msg) throws Exception {
    System.out.println("Content: " + msg.getContent());

  private void handleMultipart(Message msg) throws Exception {
    String disposition;
    BodyPart part;
    System.out.println("Class:" + msg.getContent().getClass().getName());
    System.out.println("Content:" + msg.getContent());
    Multipart mp = (Multipart)msg.getContent();
    int mpCount = mp.getCount();
    for (int m = 0; m < mpCount; m++) {
      part = mp.getBodyPart(m);
      disposition = part.getDisposition();
      if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
      } else {

  private void saveAttach(BodyPart part) throws Exception {
    String temp = part.getFileName();
//    String s = temp.substring(8, temp.indexOf("?="));
//    String fileName = base64Decoder(temp);
    String fileName = temp;
    System.out.println("File name:" + fileName);

    InputStream in = part.getInputStream();
    FileOutputStream writer = new FileOutputStream(new File("c:\\temp\\" + fileName));
    byte[] content = new byte[255];
    int read = 0;
    while ((read = in.read(content)) != -1) {

  private static String base64Decoder(String s) throws Exception {
    sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
    byte[] b = decoder.decodeBuffer(s);
    return (new String(b));

  public void receive(String username, String password) {
    String host = "";

    try {
      Properties prop = new Properties();
      prop.put("mail.pop3.host", host);
      Session session = Session.getDefaultInstance(prop);
      Store store = session.getStore("pop3");
      store.connect(host, username, password);

      Folder inbox = store.getDefaultFolder().getFolder("INBOX");

      Message[] msg = inbox.getMessages();

      FetchProfile profile = new FetchProfile();
      inbox.fetch(msg, profile);
      String str = msg.toString();
      for (int i = 0; i < msg.length; i++) {
        msg[i].setFlag(Flags.Flag.DELETED, true);
        Message ms = msg[i];
      if (inbox != null) {
      if (store != null) {
    } catch (Exception e) {


  public static String Receive() {
    RM cn = new RM();
    cn.receive("username", "pass");
    return "OK";

Oracle Database has it's own JavaMail package included to the database. And it's different from newest one. When I run procedure in JDeveloper it works. But when i try to run it like stored procedure sometimes it gives an error.
Error fires when e-mail message have attachment with content-type = "octet-stream".
Error is: "javax.mail.MessagingException: No inputstream from datasource"
I spend 2 or 3 days to find out what is the reason. I found that:
1) error happens when i try to do this:
    Multipart mp = (Multipart)msg.getContent();
    int mpCount = mp.getCount();

2) error doesn't fire when file is "text/plain" (content type)
3) i tried to replace JavaMail in the database with newest one. Result: Same error fires

and the last one 4)
If i try to import messages for the first time all works ok but for the second time it gave me an error. I've tested all of this in PL/SQL Developer with the following code:
select receivemail('','usr','pwd') from dual

But after reconnection to the database with pl/sql developer all works fine again but only for the first time.

Can someone help me: what is the reason of that error, what can i do with it, how can i replace the standard packages (jars) of oracle database (if i do something wrong)?

Please someone help me. This error makes me crazy :'( Embarassed Embarassed
Previous Topic: Problem with Oracle Hibernate 3.2.0
Next Topic: Java external procedure parameters
Goto Forum:

Current Time: Fri Jul 28 08:24:44 CDT 2017

Total time taken to generate the page: 0.19672 seconds