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 |
|
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 #627625 is a reply to message #627188] |
Thu, 13 November 2014 06:54 |
|
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
|
|
|
|
Goto Forum:
Current Time: Thu Apr 25 17:01:35 CDT 2024
|