Home » SQL & PL/SQL » SQL & PL/SQL » sending mail using java in oracle PL/SQL 11g (oracle 11g database)
sending mail using java in oracle PL/SQL 11g [message #627188] Sat, 08 November 2014 08:30 Go to next message
m7777G
Messages: 3
Registered: November 2014
Location: BANGALORE
Junior Member
I using 11g database..
I have created a procedure to send mail using java..as below





//body part added successfully


CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "SendAttach" AS
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendAttach {
public static void sendmail(String recipient,String subject,String msg,String file)
{
String host="smtp.gmail.com";
final String user="memadhuh@gmail.com";//change accordingly
final String password="paaswwrd";//change accordingly

String to=recipient;//change accordingly


//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});


//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
BodyPart messageBodyPart1 = new MimeBodyPart(); //newly added
messageBodyPart1.setText(msg); //newly added

message.setText(msg);
MimeBodyPart messageBodyPart = new MimeBodyPart();


Multipart multipart = new MimeMultipart();


messageBodyPart = new MimeBodyPart();
String file1 = file;
String fileName = "sql";
DataSource source = new FileDataSource(file1);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPart1);




message.setContent(multipart);


//send the message
//Transport.sendMessage(message,to);
Transport.send(message);


System.out.println("message sent successfully...");

} catch (MessagingException e) {e.printStackTrace();}
}
}
/

CREATE OR REPLACE
procedure Send_Attach(recipient VARCHAR,subject VARCHAR,msg VARCHAR,file VARCHAR) AS
LANGUAGE JAVA NAME 'SendAttach.sendmail (java.lang.String,java.lang.String,java.lang.String,java.lang.String)';
/

Declare
recipient varchar(50):='memadhuh@gmail.com';
subject varchar(100):='hi';
msg varchar(500):='this is a test mail with attachment';
file varchar(100):='C:/sql.txt';
Begin
Send_Attach(recipient,subject,msg,file);
End;
/
this code works fine when i connected as SYSDBA and sends mail successfully but ..
but when i connect as user as ori/ori@orcl it gives the below error

warning:java created with compilation warnings
sql>show error
error for java source
ori-29534: Referenced object ORI.javax/mail/session could not be resolved

anyone please help me to resolve the problem...


Edited by Lalit : fixed typo in topic title

[Updated on: Thu, 13 November 2014 07:53] by Moderator

Report message to a moderator

Re: sending mail using java in oracle olsql 11g [message #627190 is a reply to message #627188] Sat, 08 November 2014 08:37 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/

29534, 00000, "referenced object %s.%s could not be resolved"
// *Cause: Name resolution determined that the indicated object is referenced
//          but could not be resolved.
// *Action: Correct name resolver or address resolution problems in the
//          referenced class, or correct compilation problems in its source.

Re:ora-39534 while sending mail using java in oracle olsql 11g [message #627209 is a reply to message #627188] Sun, 09 November 2014 04:38 Go to previous messageGo to next message
m7777G
Messages: 3
Registered: November 2014
Location: BANGALORE
Junior Member
/forum/fa/12265/0/



[Edit MC: remove useless quote of first post]
  • Attachment: prs.png
    (Size: 64.44KB, Downloaded 2935 times)

[Updated on: Sun, 09 November 2014 04:57] by Moderator

Report message to a moderator

Re:ora-39534 while sending mail using java in oracle olsql 11g [message #627283 is a reply to message #627209] Mon, 10 November 2014 12:06 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
did you grant permission for the ori user to use the java code?
Re:ora-39534 while sending mail using java in oracle olsql 11g [message #627284 is a reply to message #627283] Mon, 10 November 2014 12:20 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>this code works fine when i connected as SYSDBA and sends mail successfully but
you should NEVER create new objects using SYS schema; which is only for database maintenance & upgrades.

which schema owns the java code?
Re: sending mail using java in oracle olsql 11g [message #627300 is a reply to message #627188] Mon, 10 November 2014 14:34 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
https://community.oracle.com/thread/3628357
Re: sending mail using java in oracle olsql 11g [message #627625 is a reply to message #627188] Thu, 13 November 2014 06:54 Go to previous messageGo to next message
m7777G
Messages: 3
Registered: November 2014
Location: BANGALORE
Junior Member
thanks you all for the response guys..
finally i have got solution..
please follow these steps.If you want to do the same

step 1:connect /as sysdba
step 2:grant the below permissions to the database
call dbms_java.grant_permission('ORI',
'java.util.PropertyPermission','*', 'read,write');
execute dbms_java.grant_permission('ORI','java.util.PropertyPermission','*','read');
execute dbms_java.grant_permission( 'ORI', 'SYS:java.lang.RuntimePermission', 'getClassLoader', ' ' );
execute dbms_java.grant_permission( 'ORI', 'SYS:oracle.aurora.security.JServerPermission', 'Verifier', ' ' );
execute dbms_java.grant_permission( 'ORI', 'SYS:java.lang.RuntimePermission', 'accessClassInPackage.sun.util.calendar', ' ' ) ; execute dbms_java.grant_permission( 'ORI', 'java.net.SocketPermission', '*', 'connect,resolve' );
execute dbms_java.grant_permission( 'ORI', 'SYS:java.lang.RuntimePermission', 'createClassLoader', ' ');

note:replace ORI with your DB user_name

step3: download 2 jar files jaf-1.1.1 and javamail-1.1.1.jar from oracle community network

step4: unzip this two files into into sphecific folder.so that you can find two jar files activation.jar and mail.jar

step 5:now open the command prompt in the path where the two jar files are placed in and load these two jars into DB using below command
loadjava -user U_name/PSD -resolve -synonym mail.jar
loadjava -user U_name/PSD -resolve -synonym activation.jar

step 6:now run your java procedure
[code]
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "SendAttach" AS
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendAttach {
public static void sendmail(String recipient,String subject,String msg,String file)
{
String host="smtp.gmail.com";
final String user="mail_id";//change accordingly
final String password="email_password";//change accordingly

String to=recipient;//change accordingly

//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});

//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
BodyPart messageBodyPart1 = new MimeBodyPart(); //newly added
messageBodyPart1.setText(msg); //newly added

message.setText(msg);
MimeBodyPart messageBodyPart = new MimeBodyPart();

Multipart multipart = new MimeMultipart();

messageBodyPart = new MimeBodyPart();
String file1 = file;
String fileName = "sql";
DataSource source = new FileDataSource(file1);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPart1);


message.setContent(multipart);

//send the message
//Transport.sendMessage(message,to);
Transport.send(message);

System.out.println("message sent successfully...");

} catch (MessagingException e) {e.printStackTrace();}
}
}
/
step 7:
CREATE OR REPLACE procedure Send_Attach(recipient VARCHAR,subject VARCHAR,msg VARCHAR,file VARCHAR) AS LANGUAGE JAVA NAME 'SendAttach.sendmail (java.lang.String,java.lang.String,java.lang.String,java.lang.String)';
/
step 8: say commit.
step 9:
Declare
recipient varchar(50):='recipient_id';
subject varchar(100):='hi';
msg varchar(500):='this is a test mail with attachment';
file varchar(100):='file path'; --attchment file path
Begin
dbms_java.grant_permission('ORI','java.io.FilePermission',file,'read');
Send_Attach(recipient,subject,msg,file);
End;
[\commit]
note:turn off windows firewall and anti virus before sendind mails



keep smiling..........

[Updated on: Thu, 13 November 2014 06:57]

Report message to a moderator

Re: sending mail using java in oracle olsql 11g [message #627632 is a reply to message #627625] Thu, 13 November 2014 09:14 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Is there any particular reason that you never use code tags?
Previous Topic: extract sql statements from trace file in windows environment
Next Topic: Procedure in oracle
Goto Forum:
  


Current Time: Thu Apr 25 17:01:35 CDT 2024