我正在尝试使用保存在Oracle数据库中的Java类从gmail接收电子邮件。目前,我使用Oracle Database 12.2。
这是我的Java源代码:
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMultipart;
public class GmailInbox {
public static void main(String[] args)
throws SQLException, MessagingException, IOException
{
read();
}
public static String read()
throws SQLException, MessagingException, IOException
{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "*********@gmail.com","password");
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
System.out.println("Total Messages:- " + messageCount);
Message[] messages = inbox.getMessages();
System.out.println("------------------------------");
for (int i = messages.length - 1; i >= messages.length-3 ; i--) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("Date: " + message.getReceivedDate());
System.out.println("From: " + InternetAddress.toString(message.getFrom()));
}
inbox.close(true);
store.close();
return("Done");
}
}
它在Eclipse IDE中完美运行。
我通过
将此Java Source加载到数据库中loadjava -user <user>/<password> -verbose -resolve <path_to_java_code>
类别和来源均为“有效”
select DBMS_JAVA.LONGNAME(OBJECT_NAME) as object_name,
object_type,
status,
from user_objects
where object_type like 'JAVA%';
之后,我创建了一个函数:
create or replace function TestGmail
return varchar2
is language java name
'GmailInbox.read() return String';
最后,当我尝试通过以下代码在Oracle中执行此类时:
declare
output varchar2(50);
begin
output := TestGmail;
dbms_output.put_line(output);
end;
我遇到了错误:
ORA-29532:Java调用因未捕获的Java异常而终止: javax.mail.NoSuchProviderException:没有imaps提供程序
有什么办法可以避免此错误?
答案 0 :(得分:1)
使用loadjava
utility在数据库中存储必要的依赖项(似乎是javax.mail.jar
),以便Oracle可以在其类路径中找到它们。
例如:
loadjava -user <user>/<password> -resolve javax.mail.jar