java.security.AccessControlException:访问被拒绝(" java.net.SocketPermission"" smtp.gmail.com""解决") [英] java.security.AccessControlException: access denied ("java.net.SocketPermission" "smtp.gmail.com" "resolve")

查看:325
本文介绍了java.security.AccessControlException:访问被拒绝(" java.net.SocketPermission"" smtp.gmail.com""解决")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GlassFish应用服务器3.1和Java邮件API 1.4.5。

I am using GlassFish Server 3.1 and the Java mail Api 1.4.5.

场景:我有一个小程序,可以在点击时发送一封电子邮件

Scenario: I have an applet, that when clicked it sends an email message.

发送邮件的作品完美的NetBeans的appletviewer,但是当添加到浏览器,并试图从那里发送电子邮件它变成地狱。

Send the mail works perfectly on Netbeans AppletViewer, but it turns into hell when added to the browser and trying to send the email from there.

我看了好几个小时,有关政策文件,符号/无符号的小程序...等。

I have read for hours, about policy files, signed/unsigned applets...etc.

我已经使用签名Applet(教程很多在那里签署它使用keytools从Java,是很简单的)尝试。当我在它要求允许浏览器中运行它,因为一个汇入作业自签名的证书,我给它的权限,但它仍然吐出了同样的异常。

I have tried using the signed applet (plenty of tutorials out there for signing it, was quite simple using the keytools from java). When I run it on the browser it asks for permission because it´s a self-signed certificate, I give it permission , but it still spits out the same exception.

我也尝试修改java.poilcy文件中添加

I have also tried modifying java.poilcy file adding

许可java.net.SocketPermissionsmtp.gmail.com:587,听着,解决;

permission java.net.SocketPermission "smtp.gmail.com:587", "listen,resolve";

但什么都没有。

我知道那个汇入作业异常,因为我在Java控制面板中activaded Java控制台。我真的鸵鸟政策知道自己还能做些什么。

I know it´s that exception because I activaded the Java Console in the Java Control Panel. I really don´t know what else to do.

下面是code发送的电子邮件:

Here is the code that sends the email:

    String host = "smtp.gmail.com";
    String from = *****;
    String pass = ******;
    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props, null);
    this.message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress toAddress = new InternetAddress(this.to);
    this.message.addRecipient(Message.RecipientType.TO, toAddress);

    this.message.setSubject(this.subject);

    this.message.setText(this.body);

    Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);
    transport.sendMessage(this.message, this.message.getAllRecipients());
    transport.close();


推荐答案

JApplet的是在比常规应用它自己的,因为不同的权限的沙箱(当用户选择直接这样做,因此应用程序仅执行用户接受的后果)。一个JApplet的执行,当浏览器下载它,给用户没有选择任何,that's为什么如果你希望你的小应用程序部署和其他执行(当小程序访问服务器别人比从它被部署一)必须签字(无论是自签名证书或经授权的组织,这通常意味着要支付一些费用签名的证书),使用户可以接受所述Applet使用的后果,允许它走出沙盒。

JApplet is in a "sandbox" on it's own, given different permissions than regular applications (applications are only executed when the users chooses directly to do so, hence, the user accepts the consequences). A JApplet executes when a browser downloads it, giving the user no option whatsoever, that´s why if you want to have your applet deployed and executed by others (when the applet accesses servers others than the one from which it is deployed) it must be signed (either a self-signed certificate or a certificate signed by an authorized organization, which usually implies paying some fees) so that the user can "Accept" the consequences of using said Applet, allowing it "out of the sandbox".

由于某些原因,使用keytolls和的jarsigner我无论如何没有工作自证书签名。即使当我访问网页和浏览器警告我有关执行的小程序(给我的选择不执行它)我接受了警告说,它似乎是JApplet的没有得到许可汇入作业

For some reason, signing it with a self-cert using keytolls and jarsigner did not work for me whatsoever. Even though when I accessed the webpage and the browser warned me about executing the applet (giving me the option to not execute it) and I accepted said warning, it seemed the JApplet was not getting it´s permissions.

我的男朋友提出移动电子邮件类出来的沙箱中。 他解决了它(保佑他!),移动emailClass(使用Java邮件API的)到服务器没有给出任何的问题。使用前端控制器命令客户端 - 服务器Arquitecture,我所要做的就是实现我的code,我张贴在问题之初控制器类,并从我的小程序发送(被点击的按钮时)的与toEmailAddress,主题http请求,和身体我的servlet。

My boyfriend suggested moving the email class out of the "sandbox". He solved it (bless him!), moving the emailClass (the one which uses the java mail api) to the server gave no problems whatsoever. Using the Front Controller Command for Client-Server Arquitecture, all I had to do was implement my Controller class with the code that I posted at the beginning of the question, and send from my applet (when the button was clicked) an http-request with the toEmailAddress, subject, and body to my servlet.

运行完美。

这篇关于java.security.AccessControlException:访问被拒绝(" java.net.SocketPermission"" smtp.gmail.com""解决")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆