title: How do I code my own SMTP Server using Java?
body: <p>So I have a client written in Java that i want to use to test out sending email but instead of using an already existing SMTP like google, i want to have my own local server to test out sending mock emails between two mock emails.</p>
  <p>I've been trying to look all over the internet for good sources on how to code a simple SMTP Server but i've had zero luck.</p>
  <p>I do have a basic server code that when i run it, i can connect my Client to it but at the moment it won't handle any email functionality.</p>
  <p>TCPServer.java</p>
  <pre><code>
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.net.InetAddress;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.io.*;
  import java.net.*;
  
  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 &amp;&amp; !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(&quot;\r\nNew client connection from &quot; + 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(&quot;\r\nMessage from &quot; + clientAddress + &quot;: &quot; + 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 = &quot;192.168.1.235&quot;; // local IP address
          int port = 8088;
  
          if (args.length &gt; 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(&quot;\r\nRunning Server: &quot; +
                  &quot;Host=&quot; + server.getSocketAddress().getHostAddress() +
                  &quot; Port=&quot; + server.getPort());
          server.listen();
      }
  
  }
  </code></pre>
  <p>What can i add to my existing server code to make it handle email for my Client. I'll also post my email client as well.</p>
  <p>ClientTester.java</p>
  <pre><code>
  import java.io.*;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.util.Scanner;
  
  /**
   * This program demonstrates a TCP client
   * @author jl922223
   * @version 1.0
   * @since 2020-12-12
   */
  
  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
       */
  /*    private void start() throws IOException {
          String input;
          //create a new PrintWriter from an existing OutputStream (i.e., tcpSocket).
          //This convenience constructor creates the necessary intermediateOutputStreamWriter, which will convert characters into bytes using the default character encoding
          //You may add your code in a loop so that client can keep send datagrams to server
          //add your code here
          while (true) {
              System.out.print (&quot;C:&quot;);
              input = scanner.nextLine();
              PrintWriter output = new PrintWriter(this.tcpSocket.getOutputStream(), true);
              output.println(input);
              output.flush();
          }
      }*/
  
      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(&quot;smtp.google.com&quot;); // local IP address
          int port = 25;
          if (args.length &gt; 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);
  
  //        client.start();
  
          try{
  
              client = new ClientTester(serverIP, port);
  
              System.out.println(&quot;\r\n Connected to Server: &quot; + 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 (&quot;S:&quot; + sockin.readLine ());
  
              while (true){
                  System.out.print (&quot;C:&quot;);
  
                  String cmd = stdin.readLine ();
  
                  sockout.println (cmd);
  
                  String reply = sockin.readLine ();
  
                  System.out.println (&quot;S:&quot; + reply);
                  if (cmd.toLowerCase ().startsWith (&quot;data&quot;) &amp;&amp;
                          reply.substring (0, 3).equals (&quot;354&quot;))
                  {
                      do
                      {
                          cmd = stdin.readLine ();
  
                          if (cmd != null &amp;&amp; cmd.length () &gt; 1 &amp;&amp;
                                  cmd.charAt (0) == '.')
                              cmd = &quot;.&quot;; // Must be no chars after . char.
  
                          sockout.println (cmd);
  
                          if (cmd.equals (&quot;.&quot;))
                              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 (&quot;S:&quot; + reply);
  
                      continue;
                  }
  
                  // If the QUIT command was entered, quit.
  
                  if (cmd.toLowerCase ().startsWith (&quot;quit&quot;))
                      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)
              {
              }
              }
      }
  }
  </code></pre>
  <p>The good news is that the ClientTester works when i connect it to <code>smtp.google.com</code> but i don't want to use Googles, i want to have my own basic Email server in java.</p>