Java中的简单邮件客户端 - 获取501 5.5.4无效的域名

时间:2017-11-13 23:41:41

标签: java email networking tcp smtp

我正在编写一个简单的邮件客户端,需要执行以下操作:

  • 与邮件服务器建立TCP连接。
  • 向/从邮件服务器发送/接收邮件。
  • 关闭与邮件服务器的连接。

到目前为止,我的客户端将设置连接很好,但是当它尝试发送“HELO”+我的InetAddress时,主机说域名无效。

如何解决此问题,以便我可以通过邮件客户端发送电子邮件?

这是我的代码:

    package main;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MailClient {
    private static int port;
    private static String mailServer;
    private Socket sock;
    private DataInputStream inputStream;
    private DataOutputStream outputStream;
    private BufferedReader br;
    //private OutputStreamWriter osw;
    private PrintWriter pw;

    public MailClient(String mailServer, int port) {
        this.mailServer = mailServer;
        this.port = port;
    }

    // Establish TCP connection with mail server
    public void setUpConnection() {
        System.out.println("Setting up connection to server...\n");
        try {
            sock = new Socket(mailServer, port);
            inputStream = new DataInputStream(sock.getInputStream());
            outputStream = new DataOutputStream(sock.getOutputStream());
            br = new BufferedReader(new InputStreamReader(inputStream));
            pw = new PrintWriter(outputStream);
            //osw = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);


            if(readResponse() == 220) {
                System.out.println("Connection established!\n");
            }

        } catch (UnknownHostException e) {
            System.err.println("Host name" + mailServer + " isn't recognized\n");
        } catch (IOException e) {
            System.err.println("I/O for host " + mailServer + " failed.\n");
        }
    }

    // Dialogue with mail server using SMTP
    public void sendMessage(String from, String to, String subject, String message) {

            System.out.println("Sending message to server...\n");
            try {
                String response;
                writeMsg("HELO "  + InetAddress.getLocalHost().getHostName() + "\n");
                readResponse();
                writeMsg("MAIL FROM: " + from + "\n");
                readResponse();
                writeMsg("RCPT TO: " + to + "\n");
                readResponse();
                writeMsg("DATA\n");
                readResponse();
                writeMsg("From: " + from + "\n");
                writeMsg("To: " + to + "\n");
                writeMsg("Date: " + "\n");
                writeMsg("Subject: " + subject + "\n");
                writeMsg(message + "\n");
                writeMsg("\n.\n");
                readResponse();
                writeMsg("QUIT");
                readResponse();
                System.out.println("Message sent!");

                String responseFromServer;

                while((responseFromServer = br.readLine()) != null) {
                    System.out.println("Server response: " + responseFromServer);
                    if(responseFromServer.indexOf("Ok") != -1) {
                        System.out.println("Ok recieved from mail server!");
                        break;
                    }
                }


                closeConnection();

            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    // Close TCP connection
    private void closeConnection() {
        System.out.println("Closing connection to server...");
        try {
            br.close();
            pw.close();
            inputStream.close();
            outputStream.close();
            sock.close();
            System.out.println("Connection to mail server closed.");
        }catch(IOException e) {
            System.err.println(e);
        }
    }

    private int readResponse() throws IOException {
        String line = br.readLine();
        System.out.println("Server Response:" + line + "\n");
        line = line.substring(0, line.indexOf(" "));
        return Integer.parseInt(line);
    }

    private void writeMsg(String message) {
        pw.println(message);
        pw.flush();
        System.out.println("Message to server: " + message);
    }

    public static void main(String[] args) {
        String emailAddress = "test@domain.edu";
        String subject = "test";
        String message = "hello world";
        String test= "mailserver.edu";
        MailClient mc = new MailClient(test, 587);
        mc.setUpConnection();
        mc.sendMessage("example@yahoo.com", emailAddress, subject, message);
    }
}

我收到的逐字错误:501 5.5.4 Invalid domain name

0 个答案:

没有答案