The Mail4ME Project 
by Joerg Pleumann 

Listing One
public class MailingList {
  static String pop3Host = "pop3.yourisp.com";
  static String pop3User = "user0815";
  static String pop3Pass = "secret";
  static String smtpHost = "smtp.yourisp.com";
  static String listMachine = "localhost";
  static String listOwner = "owner@yourlist.com";
  static String[] users = {"duke@sun.com", "clippy@microsoft.com"};
}

Listing Two
  public static void main(String[] args) throws Exception {
    Pop3Client pop3 = new Pop3Client();
    pop3.open(pop3Host, pop3User, pop3Pass);
    for (int i = 0; i < pop3.getMessageCount(); i++) {
      Message message = pop3.getMessage(i);
      if (!isValidUser(message)) {
        badMessage(message, "you are not subscribed to the list.");
      }
      else {
        goodMessage(message);
      }
      pop3.removeMessage(i);
    }
    pop3.close();
  }


Listing Three
static boolean isValidUser(Message message) throws Exception {
  String sender = message.getHeaderValue("From");
  sender = Message.getMachineAddress(sender).toLowerCase();
  for (int i = 0; i < users.length; i++) {
    if (users[i].equals(sender)) return true;
  }
  return false;
}


Listing Four
static void badMessage(Message message, String reason) throws Exception {
  String sender = message.getHeaderValue("From");

  Message reply = new Message(listOwner, sender, "Error");
  reply.addBodyLine("Sorry, " + Message.getDisplayAddress(sender));
  reply.addBodyLine("");
  reply.addBodyLine("your message could not be delivered because");
  reply.addBodyLine(reason);
  reply.addBodyLine("");
  reply.addBodyLine("Regards, " + owner);

  SmtpClient smtp = new SmtpClient(listMachine);
  smtp.open(smtpHost);
  smtp.sendMessage(reply);
  smtp.close();
}


Listing Five
static void goodMessage(Message message) throws Exception {
  Envelope envelope = new Envelope(message, false);
  envelope.setSender(listOwner);
  for (int i = 0; i < users.length; i++) {
    envelope.addRecipient(users[i]);
  }
  SmtpClient smtp = new SmtpClient(listMachine);
  smtp.open(smtpHost);
  smtp.sendMessage(envelope);
  smtp.close();
}


Listing Six
static boolean isTextOnly(MimeDecoder mime) {
  int count = mime.getPartCount();
  if (int i == 0) {
    for (int i = 0; i < count; i++) {
      if (!isTextOnly(mime.getPart(i))) return false;
    }
  }
  else {
    if (!mime.getType().toLowerCase().equals("text/plain")) return false;
  }
  return true;
}


Listing Seven
if (!isValidUser(message)) {
  badMessage(message, "you are not subscribed to the list.");
}
else if (!isTextOnly(new MimeDecoder(message))) {
  badMessage(message, "the list allows plain text messages only.");
}
else {
  goodMessage(message);
}




