嵌入HTML中的Java applet在发送电子邮件时会挂起浏览器

时间:2011-12-04 11:09:48

标签: java javascript applet javamail

我正在尝试创建一个网站,当有人打开它时会向我发送电子邮件。它存储在本地,所以我不能使用PHP。我决定使用Java applet并经过大量工作(Firefox对applet有一些严重的问题)它几乎可以工作。 Applet已完全加载,初始化,启动并响应html按钮。

我正在使用Javamail 1.4.4,会发生的事情是Firefox和Opera(两个浏览器以相同的方式做出反应)挂起,大约20秒后它们再次正常工作但邮件没有发送。这是堆栈跟踪:

DEBUG: setDebug: JavaMail version 1.4.4
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
network: Cache entry not found [url: http://smtp.gmail.com/crossdomain.xml, version: null]
network: Connecting http://smtp.gmail.com/crossdomain.xml with proxy=DIRECT
network: Connecting http://smtp.gmail.com:80/ with proxy=DIRECT
java.security.PrivilegedActionException: java.net.ConnectException: Connection timed out: connect
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
    at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
    at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
    at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at java.net.InetSocketAddress.<init>(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:295)
    at mai.Mai$1.run(Mai.java:66)
    at mai.Mai$1.run(Mai.java:42)
    at java.security.AccessController.doPrivileged(Native Method)
    at mai.Mai.send(Mai.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin.javascript.JSInvoke.invoke(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass$MethodInfo.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass$MemberBundle.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass.invoke0(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass.invoke(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$DefaultInvocationDelegate.invoke(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo.doObjectOp(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$LiveConnectWorker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at com.sun.deploy.net.CrossDomainXML$2.run(Unknown Source)
    ... 39 more
java.security.AccessControlException: access denied ("java.net.SocketPermission" "smtp.gmail.com" "resolve")
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkConnect(Unknown Source)
    at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
    at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at java.net.InetSocketAddress.<init>(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:295)
    at mai.Mai$1.run(Mai.java:66)
    at mai.Mai$1.run(Mai.java:42)
    at java.security.AccessController.doPrivileged(Native Method)
    at mai.Mai.send(Mai.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin.javascript.JSInvoke.invoke(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass$MethodInfo.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass$MemberBundle.invoke(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass.invoke0(Unknown Source)
    at sun.plugin2.liveconnect.JavaClass.invoke(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$DefaultInvocationDelegate.invoke(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo.doObjectOp(Unknown Source)
    at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$LiveConnectWorker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

这是我的applet的代码(我在某处读到了我应该使用doPrivileged()但它似乎没有对堆栈跟踪产生影响):

package mai;

import java.applet.Applet;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class Mai extends Applet{

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "myMail@gmail.com";
    private static final String SMTP_AUTH_PWD = "myPassword";

    @Override
    public void init(){
        System.out.println("init");
    }

    public void send(){
        System.out.println("send");

        AccessController.doPrivileged(new PrivilegedAction<String>() {

                @Override
                public String run() {
                     try{

                        Properties props = new Properties();

                        props.put("mail.transport.protocol", "smtps");
                        props.put("mail.smtps.host", SMTP_HOST_NAME);
                        props.put("mail.smtps.auth", "true");
                        // props.put("mail.smtps.quitwait", "false");

                        Session mailSession = Session.getDefaultInstance(props);
                        mailSession.setDebug(true);
                        Transport transport = mailSession.getTransport();

                        MimeMessage message = new MimeMessage(mailSession);
                        message.setSubject("Testing SMTP-SSL");
                        message.setContent("This is a test", "text/plain");

                        message.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("myMaill@gmail.com"));

                        transport.connect
                          (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

                        transport.sendMessage(message,
                            message.getRecipients(Message.RecipientType.TO));
                        transport.close();
                    }catch(Exception e){

                        e.printStackTrace();
                    }finally{
                         return "send";
                     }
                }
            });


    }
}

我还读到我可以签署我的applet,但每次我的applet加载时都会调用弹出窗口,这不是一个选项,因为它必须有点隐藏。

任何想法我做错了什么?

(如果您认为在这里使用applet太复杂了,请告诉我一个更好的方法。据我所知,JavaScript无法发送电子邮件,我没有服务器所以我不能使用php或类似的东西node.js和发送信息的所有其他库都需要从用户登录。

1 个答案:

答案 0 :(得分:1)

欢迎来到恶意代码编写者俱乐部(恶意包括跟踪)。您违反了浏览器w.r.t的同源政策。新的网络连接,这意味着您可以连接到的唯一不需要签名的地方就是为原始页面提供服务的主机。