扩展ssl/tls消息以在java程序中重新进入

vatpfxk5  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(277)

我将以下问题/解决方案扩展为重新进入者,以便使用tls加密将后续电子邮件连接/提交到smtp电子邮件服务器(如comcast)。
在java程序中发出starttls命令后,starthandshake出现不受支持或无法识别的ssl消息异常
我已经成功地研究和原型用户91556的算法,并调整它为后续电子邮件。经过多次尝试,我成功地将他/她的tls加密功能应用到了我以前的算法中,并且我的代码始终有效。。。不过,我不认为这是正确的(是的,我缺乏电子邮件服务器连接协议的基础知识,也可以使用一个参考)我不满意握手前的非依赖睡眠(即100毫秒)在我的环境中工作,但怀疑它的保证一般。我尝试在tls输入流(sslin)上循环以查看它何时“就绪”,但它总是超时。因此,100毫秒睡眠。
有没有更正式/正确的方法来管理输入流-socket和sslsocket版本?
谢谢鲍勃巴克利城堡岩石公司。。。是的,下雪了。

//------------------------------------------------------------------------------
    public class SSLComcastTLSPrototype
    {
      //----------------------------------------------------------------------------
      public static void main(String[] args) throws InterruptedException
      {
        for (int i=0; i<5; ++i)
        {
          System.out.println("----------------------------------------------");
          System.out.println("sendMessage #" + i);
          System.out.flush();

          sendMessage(i);
        }
      }

      //----------------------------------------------------------------------------
      public static void sendMessage(int msgNum)
      {
        Date        dDate       = new Date();
        DateFormat  dFormat     = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
        int         SMTPPort    = 587;
        String      SMTPServer  = "smtp.comcast.net";

        try (Socket   smtpSocket = new Socket(SMTPServer, SMTPPort);
             DataOutputStream os = new DataOutputStream(smtpSocket.getOutputStream());
             BufferedReader   is = new BufferedReader(new nputStreamReader(smtpSocket.getInputStream ())))
        {
          int i = 0;
          while (! is.ready() && ++i < 10)
            Thread.sleep(100);

          if (! is.ready())
          {
            System.err.println("BufferedReader InputStream Timed Out - Email failed");
            System.err.flush();
            return;
          }

          new Thread(new ReadInput(is)).start();

          os.writeBytes("EHLO WOPR\r\n");
          os.writeBytes("STARTTLS\r\n");

          SSLContext ctx = SSLContext.getInstance("TLSv1.2");
          ctx.init(null, null, null);

          SSLSocketFactory factory = ctx.getSocketFactory();
          try (SSLSocket sslSocket = (SSLSocket) factory.createSocket(
              smtpSocket,
              smtpSocket.getInetAddress().getHostAddress(),
              smtpSocket.getPort(),
              true))
          {
            String[] suites = sslSocket.getSupportedCipherSuites();
            sslSocket.setEnabledCipherSuites(suites);

            try (DataOutputStream sslOut = new DataOutputStream(sslSocket.getOutputStream());
                   BufferedReader sslIn = new BufferedReader(new InputStreamReader(sslSocket.getInputStream())))
            {
              Thread.sleep(100);

              sslSocket.startHandshake();

              sslOut.writeBytes("EHLO WOPR\r\n");
              sslOut.writeBytes("AUTH LOGIN\r\n");
              sslOut.writeBytes("<encoded username>=\r\n");    // Username, BASE64 encoded
              sslOut.writeBytes("<encoded password>\r\n");     // Password, BASE64 encoded
              sslOut.writeBytes("MAIL From: Joe@Comcast.net\r\n");
              sslOut.writeBytes("RCPT To: JoeCool@Comcast.net\r\n");
              //sslOut.writeBytes("RCPT Cc: <Jim@AnyCompany.com>\r\n");
              sslOut.writeBytes("DATA\r\n");
              sslOut.writeBytes("X-Mailer: Via Java\r\n");
              sslOut.writeBytes("DATE: " + dFormat.format(dDate) + "\r\n");
              sslOut.writeBytes("From: Runway <Runway@Airfield.com>\r\n");
              sslOut.writeBytes("To: Joe Pilot\r\n");
              //sslOut.writeBytes("Cc: CCDUDE <CCPerson@TheirCompany.com>\r\n");
              //sslOut.writeBytes("RCPT Bcc: BCCDude<BCC@InvisibleCompany.com>\r\n");
              sslOut.writeBytes("Subject: Foo Foo\r\n");

              sslOut.writeBytes("MIME-VERSION: 1.0\r\n");
              sslOut.writeBytes("CONTENT-TYPE: TEXT/HTML; charset=\"ISO-8859-1\"\r\n");
              sslOut.writeBytes("CONTENT-TRANSFER-ENCODING: 7bit\r\n");

              sslOut.writeBytes("\r\n");
              sslOut.writeBytes("<html>\r\n");
              //sslOut.writeBytes("<head>\r\n");
              sslOut.writeBytes("<title>G'day, mate!</title>\r\n");
              //sslOut.writeBytes("</head>\r\n");

              sslOut.writeBytes("Message #" + msgNum + "\r\n\r\n\r\n");

              DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
              sslOut.writeBytes("\r\n");
              sslOut.writeBytes("This is the introduction of my email<br>\r\n");
              sslOut.writeBytes("Heading of the Date<br>\r\n");
              sslOut.writeBytes(df.format(new Date()) + "<br><br>\r\n");
              sslOut.writeBytes("</html>\r\n");

              sslOut.writeBytes("\r\n.\r\n");
              sslOut.writeBytes("QUIT\r\n");

              //System.out.println("EMail Message Submitted Successfully");
              //System.out.flush();

              String response;
              while ((response = sslIn.readLine()) != null)
              {
                //System.out.println("response: " + response);
                //System.out.flush();

                if (response.contains("recipient invalid domain"))
                {
                  System.err.println("*****Invalid Email Address*****");
                  System.err.flush();
                }              
              }
            }
          }
        }
        catch (NoSuchAlgorithmException | KeyManagementException | IOException | InterruptedException ex)
        {
          Logger.getLogger(SSLComcastTLSPrototype.class.getName()).log(Level.SEVERE, null, ex);
        }
      }

      //----------------------------------------------------------------------------
      static class ReadInput implements Runnable 
      {
        //--------------------------------------------------------------------------
        private final BufferedReader br;

        //--------------------------------------------------------------------------
        public ReadInput(BufferedReader br)
        {
          this.br = br;
        }

        //--------------------------------------------------------------------------
        @Override
        public void run()
        {
          try
          {
            int input;
            String allInput = "";
            while ((input = br.read()) >= 0)
            {
              allInput += (char) input;

              if (allInput.toLowerCase().endsWith("start tls\r\n"))
                break;
            }
          }
          catch (IOException e)
          {
            System.err.println("*****Exception: readInput*****");
            System.err.flush();
            e.printStackTrace();
          }
        }
      }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题