简体   繁体   中英

How do I host my own SMTP Server using Java

So i want to test my own Email Client to test sending mock emails to mock addresses on my own computer but I'm struggling on how to implement the SMTP Server code into my own.

So far, i've got it so when i run the Server and i run the client, both of them can connect together on my IP, but now i'm struggling on implementing the email part in the server for the Client to communicate to.

The final output for the whole project should look a little something like this:

S: 220 [my ip]
C: HELLO [my ip]
S: 250 Hello email1@email1.com, pleased to meet you
C: MAIL FROM: <email1@email1.com>
S: 250 ok
C: RCPT TO: <email2@email2.com>
S: 250 ok
C: DATA
S: 354 End data with <CR><LF>.<CR><LF>
C: Hello ,
C: How are you today?
C: .
S: 250 ok Message accepted for delivery
C: QUIT
S: 221 [my ip] closing connection

When i use something like google's smtp server smtp.google.com in my client code, i can achieve this output, but i want to test it with my own server. I just don't know how to implement the smtp server code into my own code.

Here's the server code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer{
    private ServerSocket server;

    /**
     * The TCPServer constructor initiate the socket
     * @param ipAddress
     * @param port
     * @throws Exception
     */
    public TCPServer(String ipAddress, int port) throws Exception {
        if (ipAddress != null && !ipAddress.isEmpty())
            this.server = new ServerSocket(port, 1, InetAddress.getByName(ipAddress));
        else
            this.server = new ServerSocket(0, 1, InetAddress.getLocalHost());
    }

    /**
     * The listen method listen to incoming client's datagrams and requests
     * @throws Exception
     */

    private void listen() throws Exception {
        // listen to incoming client's requests via the ServerSocket
        //add your code here
        String data = null;
        Socket client = this.server.accept();
        String clientAddress = client.getInetAddress().getHostAddress();
        System.out.println("\r\nNew client connection from " + clientAddress);

        // print received datagrams from client
        //add your code here
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        while ( (data = in.readLine()) != null ) {
            System.out.println("\r\nMessage from " + clientAddress + ": " + data);
            client.sendUrgentData(1);
        }



    }

    public InetAddress getSocketAddress() {
        return this.server.getInetAddress();
    }

    public int getPort() {
        return this.server.getLocalPort();
    }



    public static void main(String[] args) throws Exception {
        // set the server address (IP) and port number
        //add your code here
        String serverIP = "192.168.1.235"; // local IP address
        int port = 8088;

        if (args.length > 0) {
            serverIP = args[0];
            port = Integer.parseInt(args[1]);
        }
        // call the constructor and pass the IP and port
        //add your code here
        TCPServer server = new TCPServer(serverIP, port);
        System.out.println("\r\nRunning Server: " +
                "Host=" + server.getSocketAddress().getHostAddress() +
                " Port=" + server.getPort());
        server.listen();

    }

}

And here's my client code:


import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class ClientTester{
    private Socket tcpSocket;
    private InetAddress serverAddress;
    private int serverPort;
    private Scanner scanner;

    /**
     * @param serverAddress
     * @param serverPort
     * @throws Exception
     */
    private ClientTester(InetAddress serverAddress, int serverPort) throws Exception {
        this.serverAddress = serverAddress;
        this.serverPort = serverPort;

        //Initiate the connection with the server using Socket.
        //For this, creates a stream socket and connects it to the specified port number at the specified IP address.
        //add your code here
        this.tcpSocket = new Socket(this.serverAddress, this.serverPort);
        this.scanner = new Scanner(System.in);
    }

    /**
     * The start method connect to the server and datagrams
     * @throws IOException
     */

    public static void main(String[] args) throws Exception {
        // set the server address (IP) and port number
        //add your code here
        //IP: 192.168.1.235
        //Port: 8088
        InetAddress serverIP = InetAddress.getByName("192.168.1.235"); // local IP address
        int port = 8088;
        if (args.length > 0) {
            serverIP = InetAddress.getByName(args[0]);
            port = Integer.parseInt(args[1]);
        }

        // call the constructor and pass the IP and port
        //add your code here
        ClientTester client = new ClientTester(serverIP, port);



        try{

            client = new ClientTester(serverIP, port);

            System.out.println("\r\n Connected to Server: " + client.tcpSocket.getInetAddress());

            BufferedReader stdin;
            stdin = new BufferedReader (new InputStreamReader (System.in));

            InputStream is = client.tcpSocket.getInputStream ();
            BufferedReader sockin;
            sockin = new BufferedReader (new InputStreamReader (is));

            OutputStream os = client.tcpSocket.getOutputStream();
            PrintWriter sockout;
            sockout = new PrintWriter (os, true);

            System.out.println ("S:" + sockin.readLine ());

            while (true){
                System.out.print ("C:");

                String cmd = stdin.readLine ();

                sockout.println (cmd);

                String reply = sockin.readLine ();

                System.out.println ("S:" + reply);
                if (cmd.toLowerCase ().startsWith ("data") &&
                        reply.substring (0, 3).equals ("354"))
                {
                    do
                    {
                        cmd = stdin.readLine ();

                        if (cmd != null && cmd.length () > 1 &&
                                cmd.charAt (0) == '.')
                            cmd = "."; // Must be no chars after . char.

                        sockout.println (cmd);

                        if (cmd.equals ("."))
                            break;
                    }
                    while (true);

                    // Read a reply string from the SMTP server program.

                    reply = sockin.readLine ();

                    // Display the first line of this reply string.

                    System.out.println ("S:" + reply);

                    continue;
                }

                // If the QUIT command was entered, quit.

                if (cmd.toLowerCase ().startsWith ("quit"))
                    break;
            }
        }
        catch (IOException e)
        {
            System.out.println (e.toString ());
        }
        finally
        {
            try
            {
                // Attempt to close the client socket.

                if (client != null)
                    client.tcpSocket.close();
            }
            catch (IOException e)
            {
            }
            }
    }
}

Hopefully this made some sense on what i want achieved

Don't write your own SMTP Server. There are many out there that have way more features than you will want to spend your time implementing. I use this one:

https://github.com/gessnerfl/fake-smtp-server

It has an html interface that will allow you to view the emails that it receives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM