2016-11-26 16 views
2

Androidアプリケーション経由でメールを送信しようとしていますが、GmailのメールAPIにユーザー名とパスワードが必要なので、私のサーバーからメールを送信する必要があります私のデータはコード自体にある)JavaがTomcatサーバー経由でメールを送信しています

しかし、私は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()); 
    } 

} 

と:そう

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); 
    } 
} 

、誰かがその問題を回避するためにどのようなアイデアを持っています。 より具体的には、サーバー経由でメールを送信するが、その後は何とか応答をアンドロイドクライアントに送信したので、25秒間待つ必要はありません。メインスレッドを通してそれを送信すると、あなたのアプリケーションのGUIをブロックするため

おかげ

答えて

0

あなたのAndroidアプリは、別のスレッドでリクエストを送信する必要があります。 Tomcat-Serverは、デフォルトで異なるスレッドで要求を処理します。したがって、リクエスト処理の中で別のスレッドを開始する必要はありません。このような新しいスレッドを開始することができます(java.lang.Threadのドキュメントを参照してください):

Thread theThread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      // send the request to the tomcat server 
     } 
    }); 
    theThread.start(); 
+0

私は試してみて、感謝の男 – Lagistos

関連する問題