// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;

public class Email {
   // SMTP port
   public static final int SMTP_PORT = 25;

   // SMTP server
   private static String mailHost = null;

   // socket
   private Socket mailSocket;

   // streams
   private PrintWriter outStream;
   private BufferedReader inStream;

   // set SMTP server
   public static void setMailHost(String server) {
      mailHost = server;
   }

   // open email
    public void open(String from, String to, String subject) throws Exception {
       if (mailHost == null) {
          throw new Exception("Mailhost not set");
       }
       SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzzz", Locale.US);
       mailSocket = new Socket(InetAddress.getByName(mailHost), SMTP_PORT);
       outStream = new PrintWriter(mailSocket.getOutputStream());
       inStream = new BufferedReader(new InputStreamReader(mailSocket.getInputStream()));
       receive();
       send("HELO " + InetAddress.getLocalHost().getHostAddress());
       receive();
       send("MAIL FROM: <" + from + ">");
       receive();
       send("RCPT TO: <" + to + ">");
       receive();
       send("DATA");
       receive();
       send("Return-Path: <" + from + ">");
       send("From: " + from);
       send("To: " + to);
       send("Subject: " + subject);
       send("Date: " + format.format(new Date()));
       send("MIME-version: 1.0");
       send("Content-type: text/plain; Charset=iso-8859-1");
       send("Content-transfer-encoding: 8bit");
       send("");
       return;
   }

   // send line
   public void addLine(String line) {
      send(line);
      return;
   }

   // close email
   public void close() throws Exception{
      send(".");
      receive();
      send("QUIT");
      receive();
   }

   // send simple email
   public static void sendSimple(String from, String to, String subject, String body) throws Exception {
      Email temp = new Email();
      temp.open(from,to,subject);
      temp.addLine(body);
      temp.close();
      return;
   }

   // send simple email with no body
   public static void sendSimpleNoBody(String from, String to, String subject) throws Exception {
      Email temp = new Email();
      temp.open(from,to,subject);
      temp.close();
      return;
   }

   // send and receive
   private void send(String cmd) {
      outStream.print(cmd + "\r\n");
      outStream.flush();
      return;
   }

   private void receive() throws Exception{
      String resp = inStream.readLine();
      int code = Integer.parseInt(resp.substring(0,3));
      if( (code != 220) &&
          (code != 221) &&
          (code != 250) &&
          (code != 251) &&
          (code != 354) ) {
         throw new Exception("Bad response from SMTP server");
      }
      return;
   }
}
