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 {
this.handle(msg);
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++) {
this.handle(msg);
part = mp.getBodyPart(m);
disposition = part.getDisposition();
if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
saveAttach(part);
} else {
System.out.println(part.getContent());
}
}
}
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) {
writer.write(content);
}
writer.close();
in.close();
}
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 = "127.0.0.1";
try {
Properties prop = new Properties();
prop.put("mail.pop3.host", host);
Session session = Session.getDefaultInstance(prop);
session.setDebug(true);
Store store = session.getStore("pop3");
store.connect(host, username, password);
Folder inbox = store.getDefaultFolder().getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message[] msg = inbox.getMessages();
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(msg, profile);
String str = msg.toString();
for (int i = 0; i < msg.length; i++) {
msg[i].setFlag(Flags.Flag.DELETED, true);
System.out.println("Deleted");
Message ms = msg[i];
this.handleMultipart(ms);
System.out.println("****************************");
}
if (inbox != null) {
inbox.close(true);
}
if (store != null) {
store.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
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('127.0.0.1','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 :'(