jQuery in Action, 3rd edition
The moose likes EJB and other Java EE Technologies and the fly likes problem of javamail within ejb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "problem of javamail within ejb" Watch "problem of javamail within ejb" New topic

problem of javamail within ejb

Ryan Crow

Joined: Aug 19, 2002
Posts: 18
im writing a sesson bean business method enabling users to send outgoing mail thru my smtp server. the same code works fine as an external class by itself, but i encountered dis exception :
java.security.AccessControlException: access denied(java.util.PropertyPermission * read,write)
when i tried to execute the :
Properties props = System.getProperties();
putting the code as a sesson bean's business method. im green to ejbs, can anone help me here? and it's to know that i did import the classes needed for mailing.

Peter Reinhardt
Ranch Hand

Joined: Aug 02, 2002
Posts: 57
the method System.getProperties() is certainly allowed in EJBs, the java.policy for your application server (which one ?) might be too restrictive.
I haven't used JavaMail in an EJB so I don't know if it works alright.

SCJP 1.2, SCJD, SCEA, IBM 484, Weblogic 7
Ryan Crow

Joined: Aug 19, 2002
Posts: 18
thanks for the idea, peter. but do u think it has more to do with the beans container setting or the RI app server im using intergrated with forte j2ee?
here's the code of the sendmail function as a business method in a session bean :
public void sendMail(java.util.Vector to, java.lang.String topic, org.w3c.dom.Text content, java.lang.String sender) {
//defining smtp host ip
String host = "";
try {
// Get system properties :: the exception occurs here...
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(sender));
// Set the list of receipant to address
int numSub = to.size();
for (int i=0;i<numSub;i++) {
try {
String newEmail = to.elementAt(i).toString();
System.out.println("Subscriber "+i+" is "+newEmail);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(newEmail)); }
catch (Exception e) {System.out.println("error in adding recipient : "+e);}
// Set the subject
// Set the content
// Send message
System.out.println("Email has been sent");
catch (Exception e) {System.out.println("error in sending mail : "+e);}
if some how this really doesnt work out... is there any other suggested way around to comply to the same purpose?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Why not try using a JavaMail session that's set up in your environment naming context (JNDI)? That is the J2EE way of doing it. So, instead of reading the sytem properties, you would just grab the session right out of JNDI...
Context enc = ( Context )new InitialContext().lookup( "jdbc:comp/env" );
Session session = ( Session )enc.lookup( "mail/MySession" );

James Carman, President<br />Carman Consulting, Inc.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Your name ryanc does not comply with the JavaRanch naming policy. We require the displayed names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please spare a moment and modify your profile to use a publicly displayed name that meets these requirements.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link: http://aspose.com/file-tools
subject: problem of javamail within ejb
jQuery in Action, 3rd edition