Java - 不使用 JavaMail 发送电子邮件 - EHLO 客户端/服务器响应

问题描述 投票:0回答:1

我一直在网上搜索并进行连续 6 个小时的测试,但我无法让它发挥作用。我想使用 JavaMail,但是我的教授希望我们使用套接字...我只是想用 java 发送一封电子邮件到 Gmail 帐户。

无论如何,我开始使用 EHLO 而不是 HELO 来向服务器发送消息,因为从我读到的内容来看,它是“较新的”。然而,我发现自己陷入困境。在我的代码的中间点,您会注意到它全部被注释掉,直到我关闭套接字。这是我能从服务器得到好的回复。

让我在这里向您展示我的代码:

public class MailComponent{

final static String MAIL_SERVER = "smtp.gmail.com";
final static int SMTP_PORT = 25;
Socket socket = null;

public MailComponent(){
    // Making Constructor Later
}

public void sendMail() throws Exception{

    try{
        // Establish TCP Connection With Mail Server
        socket = new Socket(MAIL_SERVER, SMTP_PORT);

        // Create a Buffered Reader to read one line at a time
        InputStream inStream = socket.getInputStream();
        InputStreamReader inReader = new InputStreamReader(inStream);
        BufferedReader bReader = new BufferedReader(inReader);

        // Read Server's Greeting
        String serverResponse = bReader.readLine();
        if (!serverResponse.startsWith("220")){
            throw new Exception("220 Reply Not Received From Server!");
        }
        System.out.println(serverResponse);

        // Reference socket's output stream
        OutputStream oStream = socket.getOutputStream();

        // Send EHLO command
        String heloCommand = "EHLO smtp.gmail.com\r\n";
        oStream.write(heloCommand.getBytes("US-ASCII"));
        serverResponse = bReader.readLine();
        if (!serverResponse.startsWith("250"))
            throw new Exception("250 Reply Not Received From Server!");
        System.out.println(serverResponse);

        // Get Responses...
        serverResponse = bReader.readLine();
        System.out.println("Server Response: " + serverResponse);
        serverResponse = bReader.readLine();
        System.out.println("Server Response: " + serverResponse);
        serverResponse = bReader.readLine();
        System.out.println("Server Response: " + serverResponse);
        serverResponse = bReader.readLine();
        System.out.println("Server Response: " + serverResponse);
        serverResponse = bReader.readLine();
        System.out.println("Server Response: " + serverResponse);
        serverResponse = bReader.readLine();
        System.out.println("Server Response: " + serverResponse);
        serverResponse = bReader.readLine();
        System.out.println("Final Response: " + serverResponse);
        // END EHLO Responses

        // Send TLS command
        String tlsCommand = ("STARTTLS" + "\r\n");
        oStream.write(tlsCommand.getBytes("US-ASCII"));
        serverResponse = bReader.readLine();
        System.out.println("STARTTLS Response: " + serverResponse);



        /* Commented out

        //Send the MAIL FROM command
        String fromCommand = ("MAIL FROM: <[email protected]>\r\n");
        oStream.write(fromCommand.getBytes("US-ASCII"));
        serverResponse = bReader.readLine();
        System.out.println("Mail From Reponse: " + serverResponse);

        // Send RCPT TO command
        String toCommand = ("RCPT TO: <[email protected]>\r\n");
        oStream.write(toCommand.getBytes("US-ASCII"));
        //serverResponse = bReader.readLine();
        //System.out.println("RCPT To Response: " + serverResponse);

        // Send DATA command
        String dataCommand = ("DATA\r\n");
        oStream.write(dataCommand.getBytes("US-ASCII"));
        serverResponse = bReader.readLine();
        System.out.println("Data Response: "+ serverResponse);

        // Subject 
        String subCommand = ("SUBJECT: Email Test\r\n");
        oStream.write(subCommand.getBytes("US-ASCII"));
        //TimeUnit.SECONDS.sleep(1);
        String msg = ("Some random Message");
        oStream.write(msg.getBytes("US-ASCII"));
        //TimeUnit.SECONDS.sleep(1);

        // End Message data
        String endMsg = ("\r\n.\r\n");
        oStream.write(endMsg.getBytes("US-ASCII"));
        serverResponse = bReader.readLine();
        System.out.println("End Data Command: "+ serverResponse);

        // Send QUIT message
        String quitMsg = ("QUIT\r\n");
        oStream.write(quitMsg.getBytes("US-ASCII"));
        serverResponse = bReader.readLine();
        System.out.println("Quit Command: "+ serverResponse);


        End comment*/



        if (socket != null){
            socket.close();
            System.out.println("Closed Socket Connection!");
        }
    }
    catch(IOException ioe){System.err.println(ioe);}
}

}

请注意,我的输出中的“最终响应”在 250 和 SMTPUTF8 之间没有连字符。这就是我知道这是我必须回复之前的最后一个回复。一旦我发送了 STARTTLS 命令,我完全不知道该怎么做。

对于我应该在 STARTTLS 命令(我所有评论之前的最后一个命令)之后应该放置什么内容,我真的很感激一些帮助。预先感谢!

java sockets email smtp client-server
1个回答
0
投票

不要发送“STARTTLS”,您的代码没问题

© www.soinside.com 2019 - 2024. All rights reserved.