Home » Developer & Programmer » JDeveloper, Java & XML » error occurring in java source (windows xp , oracle 9i realese 2 , 9i jdk)
error occurring in java source [message #301518] Wed, 20 February 2008 23:20 Go to next message
jagadeeshg
Messages: 14
Registered: December 2007
Location: BLR
Junior Member

Hi

I am using javamail to send attachments.

I used the following steps to create java source.

On Windows XP
loadjava.bat -user sys/password -resolve -synonym activation.jar
loadjava.bat -user sys/password -resolve -synonym mail.jar
From SQLPLUS
sqlplus /nolog
connect sys/manager as sysdba;
SQL> call sys.dbms_java.loadjava('-v -r -grant PUBLIC -synonym jaf-1.0.1\activation.jar');
SQL> call sys.dbms_java.loadjava('-v -r -grant PUBLIC -synonym javamail-1.2\mail.jar');

sqlplus /nolog
connect sys/manager as sysdba;
SQL> exec dbms_java.grant_permission('CMS','java.util.PropertyPermission','*','read,write');
SQL> exec dbms_java.grant_permission('CMS','java.net.SocketPermission','*','connect, resolve');
SQL> exec dbms_java.grant_permission('CMS','java.io.FilePermission','C:\Usr\tmp\*','read, write');
Install Java Code to send mail with attachments
Next, the following SQL*PLUS script should be executed. It simply creates a Java class named SendMail with only one member function called Send(), and a PL/SQL package SendMailJPkg. These form an interface to JavaMail. At the end of the script, an anonymous PL/SQL block tests the whole program.
sqlplus scott/tiger
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "SendMail" AS
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMail {
// Sender, Recipient, CCRecipient, and BccRecipient are comma-separated
// lists of addresses. Body can span multiple CR/LF-separated lines.
// Attachments is a ///-separated list of file names.
public static int Send(String SMTPServer,
String Sender,
String Recipient,
String CcRecipient,
String BccRecipient,
String Subject,
String Body,
String ErrorMessage[],
String Attachments) {
// Error status;
int ErrorStatus = 0;

// Create some properties and get the default Session;
Properties props = System.getProperties();
props.put("mail.jagadeesh.com", SMTPServer);
Session session = Session.getDefaultInstance(props, null);

try {
// Create a message.
MimeMessage msg = new MimeMessage(session);

// extracts the senders and adds them to the message.
// Sender is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
msg.addFrom(TheAddresses);
}

// Extract the recipients and assign them to the message.
// Recipient is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
msg.addRecipients(Message.RecipientType.TO,TheAddresses);
}

// Extract the Cc-recipients and assign them to the message;
// CcRecipient is a comma-separated list of e-mail addresses as per RFC822
if (null != CcRecipient) {
InternetAddress[] TheAddresses = InternetAddress.parse(CcRecipient);
msg.addRecipients(Message.RecipientType.CC,TheAddresses);
}

// Extract the Bcc-recipients and assign them to the message;
// BccRecipient is a comma-separated list of e-mail addresses as per RFC822
if (null != BccRecipient) {
InternetAddress[] TheAddresses = InternetAddress.parse(BccRecipient);
msg.addRecipients(Message.RecipientType.BCC,TheAddresses);
}

// Subject field
msg.setSubject(Subject);

// Create the Multipart to be added the parts to
Multipart mp = new MimeMultipart();

// Create and fill the first message part
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(Body);

// Attach the part to the multipart;
mp.addBodyPart(mbp);
}

// Attach the files to the message
if (null != Attachments) {
int StartIndex = 0, PosIndex = 0;
while (-1 != (PosIndex = Attachments.indexOf("///",StartIndex))) {
// Create and fill other message parts;
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds =
new FileDataSource(Attachments.substring(StartIndex,PosIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
PosIndex += 3;
StartIndex = PosIndex;
}
// Last, or only, attachment file;
if (StartIndex < Attachments.length()) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(Attachments.substring(StartIndex));
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}

// Add the Multipart to the message
msg.setContent(mp);

// Set the Date: header
msg.setSentDate(new Date());

// Send the message;
Transport.send(msg);
} catch (MessagingException MsgException) {
ErrorMessage[0] = MsgException.toString();
Exception TheException = null;
if ((TheException = MsgException.getNextException()) != null)
ErrorMessage[0] = ErrorMessage[0] + "\n" + TheException.toString();
ErrorStatus = 1;
}
return ErrorStatus;
} // End Send Class
} // End of public class SendMail
/
Java created.

SQL> show errors java source "SendMail"
No errors.
Install PL/SQL Package which forms an interface to JavaMail
CREATE OR REPLACE PACKAGE SendMailJPkg AS
-- EOL is used to separate text line in the message body
EOL CONSTANT STRING(2) := CHR(13) || CHR(10);

TYPE ATTACHMENTS_LIST IS TABLE OF VARCHAR2(4000);

-- High-level interface with collections
FUNCTION SendMail(SMTPServerName IN STRING,
Sender IN STRING,
Recipient IN STRING,
CcRecipient IN STRING DEFAULT '',
BccRecipient IN STRING DEFAULT '',
Subject IN STRING DEFAULT '',
Body IN STRING DEFAULT '',
ErrorMessage OUT STRING,
Attachments IN ATTACHMENTS_LIST DEFAULT NULL) RETURN NUMBER;
END SendMailJPkg;
/
CREATE OR REPLACE PACKAGE BODY SendMailJPkg AS
PROCEDURE ParseAttachment(Attachments IN ATTACHMENTS_LIST,
AttachmentList OUT VARCHAR2) IS
AttachmentSeparator CONSTANT VARCHAR2(12) := '///';
BEGIN
-- Boolean short-circuit is used here
IF Attachments IS NOT NULL AND Attachments.COUNT > 0 THEN
AttachmentList := Attachments(Attachments.FIRST);
-- Scan the collection, skip first element since it has been
-- already processed;
-- accommodate for sparse collections;
FOR I IN Attachments.NEXT(Attachments.FIRST) .. Attachments.LAST LOOP
AttachmentList := AttachmentList || AttachmentSeparator || Attachments(I);
END LOOP;
ELSE
AttachmentList := '';
END IF;
END ParseAttachment;

-- Forward declaration
FUNCTION JSendMail(SMTPServerName IN STRING,
Sender IN STRING,
Recipient IN STRING,
CcRecipient IN STRING,
BccRecipient IN STRING,
Subject IN STRING,
Body IN STRING,
ErrorMessage OUT STRING,
Attachments IN STRING) RETURN NUMBER;

-- High-level interface with collections
FUNCTION SendMail(SMTPServerName IN STRING,
Sender IN STRING,
Recipient IN STRING,
CcRecipient IN STRING,
BccRecipient IN STRING,
Subject IN STRING,
Body IN STRING,
ErrorMessage OUT STRING,
Attachments IN ATTACHMENTS_LIST) RETURN NUMBER IS
AttachmentList VARCHAR2(4000) := '';
AttachmentTypeList VARCHAR2(2000) := '';
BEGIN
ParseAttachment(Attachments,AttachmentList);
RETURN JSendMail(SMTPServerName,
Sender,
Recipient,
CcRecipient,
BccRecipient,
Subject,
Body,
ErrorMessage,
AttachmentList);
END SendMail;

-- JSendMail's body is the java function SendMail.Send()
-- thus, no PL/SQL implementation is needed
FUNCTION JSendMail(SMTPServerName IN STRING,
Sender IN STRING,
Recipient IN STRING,
CcRecipient IN STRING,
BccRecipient IN STRING,
Subject IN STRING,
Body IN STRING,
ErrorMessage OUT STRING,
Attachments IN STRING) RETURN NUMBER IS
LANGUAGE JAVA
NAME 'SendMail.Send(java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String[],
java.lang.String) return int';
END SendMailJPkg;
/
Package created.
Package body created.

Through SQLPLUS when I try to run this I am getting errors.

cms-dtatest>var ErrorMessage VARCHAR2(4000);
cms-dtatest>var ErrorStatus NUMBER;
cms-dtatest>SET SERVEROUTPUT ON
cms-dtatest>exec dbms_java.set_output(5000);

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.07
cms-dtatest>BEGIN
2 :ErrorStatus := SendMailJPkg.SendMail(
3 SMTPServerName => '192.168.61.200',
4 Sender => 'bc_jagadeeshg@tkap.co.in',
5 Recipient => 'bc_anil@tkap.co.in',
6 CcRecipient => '',
7 BccRecipient => '',
8 Subject => 'This is the subject line: Test JavaMail',
9 Body => 'This is the body: Hello, this is a test' ||
10 SendMailJPkg.EOL || 'that spans 2 lines',
11 ErrorMessage => :ErrorMessage,
12 Attachments => SendMailJPkg.ATTACHMENTS_LIST(
13 'C:\Usr\tmp\po001.txt',
14 'C:\Usr\tmp\cc004.txt'
15 )
16 );
17 END;
18 /
BEGIN
*
ERROR at line 1:
ORA-29541: class CMS.SendMail could not be resolved
ORA-06512: at "CMS.SENDMAILJPKG", line 45
ORA-06512: at "CMS.SENDMAILJPKG", line 45
ORA-06512: at line 2


Elapsed: 00:00:05.08
Re: error occurring in java source [message #301583 is a reply to message #301518] Thu, 21 February 2008 01:30 Go to previous message
Frank
Messages: 7880
Registered: March 2000
Senior Member
I'm not sure, but shouldn't you RESOLVE the class to make it visible? Or is that ancient Oracle-java syntax?
(Haven't worked with java inside the db)
Previous Topic: loading xml file
Next Topic: Cursor FOR LOOP Issue
Goto Forum:
  


Current Time: Thu Dec 08 12:49:00 CST 2016

Total time taken to generate the page: 0.29832 seconds