Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem of javamail within ejb

 
Ryan Crow
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

thanks,
ryanc
 
Peter Reinhardt
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Peter
 
Ryan Crow
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = "192.168.1.120";
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
message.setSubject(topic);
// Set the content
//message.setText(content.toString());
message.setText("testing,testing");
// Send message
Transport.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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" );
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic