tags:

views:

153

answers:

5

UPDATE: The Code working when I use it as java program. And When I run it as jsp it gives the following exception. java.security.AccessControlException: access denied (java.security.SecurityPermission insertProvider.SunJSSE)

I posted the code below. I got the following exception.How to resolve this? I already searched in google but did not find the solution :( **java.security.AccessControlException:

<%@ page import="java.security.*" %>
    <%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<html>
<body>
<%
String name=request.getParameter("name");
String from=request.getParameter("mail");
String message1=request.getParameter("msg");
try{   
String toAddress="[email protected]";
String fromAddress=from;
String fromName=name;
String messageSubject="feedback"; 
String messageBody1=message1;
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  Properties props=new Properties();  
  props.put("mail.smtp.host","smtp.gmail.com"); 
       props.put("mail.debug","true"); 
    props.put("mail.smtp.starttls.enable","true"); 
Session session1 = Session.getDefaultInstance(props,new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "Password");
}
});
  Message message=new MimeMessage(session1);
  message.setFrom(new InternetAddress( fromAddress, fromName));
  message.setRecipient(Message.RecipientType.TO,new InternetAddress( toAddress));
  message.setSubject( messageSubject);
  message.setText( messageBody1);
  message.setSentDate(new Date());
  Transport.send(message);
 }
catch(Exception e)
{out.println(e);
     }    
%>
</body>
</html>

Help Me to fix this error

A: 

I don't think adding a security provider is required for sending mail. Also, using com.sun. classes is generally undesirable.

Here and here are ways to send mail using JavaMail.

But I recommend commons-mail.

Bozho
If I remove the security provider I got following exception. I already checked that the links you give.That gives an exception related to tls property. I set that too.After that I got the same exception.java.lang.SecurityException: Access to default session denied
Senthil
is it possible to use commons-mail in jsp? I use SUn App server
Senthil
yes, it is. Although you seem to have a general security problem, rather than one with the mailing API.
Bozho
Again When I am trying in different manner.. got this exception...java.security.AccessControlException: access denied (java.security.SecurityPermission insertProvider.SunJSSE)
Senthil
As I said, your problem is more generic and roots in the security settings of your application server. Check http://docs.sun.com/app/docs/doc/819-3664/abgjs?a=view
Bozho
thanks for the link Mr Bozho. I checked the server.log, there is no such exception recorded as in the webpage you gave. I modified the server.policy as well the client policy..Same Exception I am getting..
Senthil
A: 

Any Other Suggestions ?

Senthil
A: 

I'm sending mail using JavaMail via Gmail's smtp from my application and it works fine. I don't use any security provider, I simply add these properties to the message properties:

props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.auth","true"); 
props.put("mail.mail.smtp.ssl.enable","true");

That's it. Mail is sent with no problem. Note however, that Gmail seems to want you to login into the webmail page every once in a while, or it will stop accepting your mail via smtp (I'm not sure 100% of this, it happened once to me).

Alex
A: 

java.security.AccessControlException: access denied (java.security.SecurityPermission insertProvider.SunJSSE)

Any other steps do I need to take?

Senthil
why that -1 rating???
Senthil
because this is not a forum. I guess you saw the warning message that appeared when clicking "Add answer". It said to use comments. Because answers are for providing answers, not updating the question - for that you can edit your initial question, or ask antoher.
Bozho
That;s Ok.then how could I get members attention?
Senthil
dig further and update your question showing more knowledge on the matter than before you asked - your edit will bump the question up, and if someone feels like there is enough information, he will answer.
Bozho
Ok..I will update..
Senthil
A: 

When I am trying this snippnet in java it is working. When I am converting that in jsp with little modifications as shown in code it shows the exception

java.security.AccessControlException: access denied (java.security.SecurityPermission insertProvider.SunJSSE)

whether it is not possible to send email from jsp? this is the first time for me sending mail from jsp. I successfully run that program in java.

Senthil