Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java通过tomcat服务器发送邮件_Java_Jakarta Mail - Fatal编程技术网

Java通过tomcat服务器发送邮件

Java通过tomcat服务器发送邮件,java,jakarta-mail,Java,Jakarta Mail,我正在尝试通过android应用程序发送邮件,邮件必须通过我的服务器发送(因为Gmail的邮件API需要用户名和密码,所以我不希望我的数据包含在代码中) 但我得到了socketTimeOutException的这个错误。 我可以增加超时时间,但是在收到响应之前我什么都做不了,这需要25秒。 如果我把发送函数放在一个线程中,它根本不会发送邮件 这是服务器的代码: protected void doPost(HttpServletRequest request, HttpServletRespons

我正在尝试通过android应用程序发送邮件,邮件必须通过我的服务器发送(因为Gmail的邮件API需要用户名和密码,所以我不希望我的数据包含在代码中)

但我得到了socketTimeOutException的这个错误。 我可以增加超时时间,但是在收到响应之前我什么都做不了,这需要25秒。 如果我把发送函数放在一个线程中,它根本不会发送邮件

这是服务器的代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    JSONObject res = new JSONObject();
    try {
        System.out.println("Post ForgotMypass");
        Mongo mongo = new Mongo(LogIn.host, 27017);
        DB db = mongo.getDB("Users");
        DBCollection collection = db.getCollection("usersCollection");

        // get a myUser object and cheack for jobs
        String json = request.getParameter("JSONObj");
        JSONParser jp = new JSONParser();
        JSONObject temp = (JSONObject) jp.parse(json);

        String mail = (String) temp.get("mail");
        if (mail != null) {
            BasicDBObject searchQuer = new BasicDBObject();
            searchQuer.put("Mail", mail);
            DBObject Item = collection.findOne(searchQuer);
            if (Item != null) {

                JSONParser jp2 = new JSONParser();
                JSONObject Is = (JSONObject) jp2.parse(Item.toString());
                JSONObject I = (JSONObject) Is.get("_id");
                String id = (String) I.get("$oid");

                if (id != null) {
                    String Dmail = URLDecoder.decode(mail, "UTF-8");

                    SendMailSSL.SendMail(Dmail, 4, id);

                    StringWriter out = new StringWriter();
                    res.put("Status", "true");
                    res.writeJSONString(out);
                    response.getOutputStream().println(out.toString());
                    System.out.println("mail sent succesfully");
                } else {
                    StringWriter out = new StringWriter();
                    res.put("Status", "falseNoMail");
                    System.out.println("ver false no mail");
                    res.writeJSONString(out);
                    response.getOutputStream().println(out.toString());
                }
            } else {
                StringWriter out = new StringWriter();
                res.put("Status", "ObjectdoesntExists");
                System.out.println("ver ObjectdoesntExists");
                res.writeJSONString(out);
                response.getOutputStream().println(out.toString());
            }
        } else {// id is null
            StringWriter out = new StringWriter();
            res.put("Status", "falseIdNUll");
            System.out.println("ver falseIdNUll");

            res.writeJSONString(out);
            response.getOutputStream().println(out.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
        StringWriter out = new StringWriter();
        res.put("Status", "false");
        System.out.println("ver false");

        res.writeJSONString(out);
        response.getOutputStream().println(out.toString());
    }

}
以及:

所以,有人知道如何避免这个问题。 更具体地说,通过服务器发送邮件,但在那之后,我以某种方式将响应发送到android客户端,因此他不必等待25秒


谢谢

您的android应用程序应该在一个单独的线程中发送请求,因为通过主线程发送请求会阻塞应用程序的GUI。默认情况下,Tomcat服务器在不同的线程中处理请求。因此,您不需要在请求处理内部启动单独的线程。您可以像这样启动一个新线程(参见
java.lang.thread
)的文档:

public static void SendMail(String to, int typeOfMessage, String UserID) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("username", "password");
        }
    });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("adress"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        /*
         * all the type of messages
         */
        if (typeOfMessage == 1) {
            message.setSubject("title");
            message.setText("text");
        }else if (typeOfMessage == 2){ 
            message.setSubject("title");
            message.setText("text");
        }else if (typeOfMessage == 3){ 
            message.setSubject("title");
            message.setText("text");
        }else if (typeOfMessage == 4){ 
            message.setSubject("title");
            message.setText("text");
        }




        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
    Thread theThread = new Thread(new Runnable() {
        @Override
        public void run() {
            // send the request to the tomcat server
        }
    });
    theThread.start();