Green mail java code

A blast from the past!

"Green Mail" is a classic Java applet that was popular in the late 1990s and early 2000s. It's a simple email client that allows users to send and receive emails using a Java applet. Here's a basic implementation of Green Mail in Java:

Note: This code is for educational purposes only and should not be used for production email services.

GreenMail.java

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class GreenMail extends Applet implements ActionListener {
    private TextField fromField;
    private TextField toField;
    private TextField subjectField;
    private TextArea messageArea;
    private Button sendButton;
    private Button closeButton;
    private String mailServer = "smtp.example.com";
    private String mailUser = "[email protected]";
    private String mailPass = "your_password";

    public void init() {
        setLayout(new BorderLayout());
        add(new HeaderPanel(), BorderLayout.NORTH);
        add(new MessagePanel(), BorderLayout.CENTER);
        add(new FooterPanel(), BorderLayout.SOUTH);
    }

    private class HeaderPanel extends Panel {
        public HeaderPanel() {
            setLayout(new FlowLayout());
            fromField = new TextField(20);
            toField = new TextField(20);
            subjectField = new TextField(20);
            add(new Label("From:"));
            add(fromField);
            add(new Label("To:"));
            add(toField);
            add(new Label("Subject:"));
            add(subjectField);
        }
    }

    private class MessagePanel extends Panel {
        public MessagePanel() {
            setLayout(new BorderLayout());
            messageArea = new TextArea(10, 40);
            add(new ScrollPane(messageArea), BorderLayout.CENTER);
        }
    }

    private class FooterPanel extends Panel {
        public FooterPanel() {
            setLayout(new FlowLayout());
            sendButton = new Button("Send");
            closeButton = new Button("Close");
            add(sendButton);
            add(closeButton);
            sendButton.addActionListener(this);
            closeButton.addActionListener(this);
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sendButton) {
            sendEmail();
        } else if (e.getSource() == closeButton) {
            dispose();
        }
    }

    private void sendEmail() {
        String from = fromField.getText();
        String to = toField.getText();
        String subject = subjectField.getText();
        String message = messageArea.getText();

        try {
            // Create a socket to connect to the mail server
            Socket socket = new Socket(mailServer, 25);

            // Send the email
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.println("HELO " + mailServer);
            out.println("MAIL FROM: " + from);
            out.println("RCPT TO: " + to);
            out.println("DATA");
            out.println("Subject: " + subject);
            out.println(message);
            out.println(".");

            // Close the socket
            socket.close();
        } catch (IOException ex) {
            System.out.println("Error sending email: " + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        GreenMail applet = new GreenMail();
        applet.init();
        applet.start();
    }
}

How to run:

  1. Compile the code using javac GreenMail.java.
  2. Run the applet using appletviewer GreenMail.class.
  3. The applet will launch in a browser window. You can enter your email address, recipient's email address, subject, and message, and then click the "Send" button to send the email.

Note: This code is a basic implementation and may not work with all email servers or configurations. Additionally, it's not secure and should not be used for production email services.