Sending E-mail from Java Applet

Sending E-mail from Java Applet

Question:
How do I send e-mail from Java? Do I have to open a socket to a specific port on my server (and if so, which port and what should the data stream look like)? Or is there an easier way? Are there any restrictions on doing this from an applet?

Answer:
If the SMTP server is enabled on the machine thatruns your Web server, the following appletshould be able to generate e-mail.

The essence of the applet is the sendMsg() method,which opens a socket to port 25 of the appletsource (this is the standard SMTP port), feeds the expected SMTP header to this port, dumps the contents of a TextArea into the port (your message), then adds the required period and QUIT command.

*/import java.awt.*;import java.applet.*;import java.net.*;import java.io.*;public class Mailer extends Applet {   private int SMTP_PORT = 25;   private String appletSource = “zanzibar.ucsc.edu”;   private TextArea MsgArea;   private TextField senderField, recipientField, hostField;   public void init() {      setLayout(new BorderLayout());      Panel fields = new Panel();      fields.setLayout(new GridLayout(3, 1));      Panel recPanel = new Panel();      recPanel.setLayout(new GridLayout(2, 1));      recPanel.add(new Label(“Recipient”));      recipientField = new TextField(“”);      recPanel.add(recipientField);      fields.add(recPanel);      Panel sendPanel = new Panel();      sendPanel.setLayout(new GridLayout(2, 1));      sendPanel.add(new Label(“Sender”));      senderField = new TextField(“president”);      sendPanel.add(senderField);      fields.add(sendPanel);      Panel hostPanel = new Panel();      hostPanel.setLayout(new GridLayout(2, 1));      hostPanel.add(new Label(“Host”));      hostField = new TextField(“whitehouse.gov”);      hostPanel.add(hostField);      fields.add(hostPanel);      add(“North”, fields);      MsgArea = new TextArea();      add(“Center”, MsgArea);          add(“South”, new Button(“SEND”));   }   public boolean handleEvent(Event e) {      if (e.id == Event.WINDOW_DESTROY)         System.exit(0);      return super.handleEvent(e);   }   public boolean action(Event e, Object arg) {      if (arg.equals(“SEND”))         sendMsg(senderField.getText(), recipientField.getText(),hostField.getText());      else         return super.action(e, arg);      return true;   }   private void sendMsg(String sender, String recipient, String senderHost) {      try {         Socket s = new Socket(appletSource, SMTP_PORT);         PrintStream out = new PrintStream(s.getOutputStream());         MsgArea.selectAll();         out.println(“HELO ” + senderHost); // SMTP doesn’t verify sender host!         out.println(“MAIL FROM: ” + sender);         out.println(“RCPT TO: ” + recipient);         out.println(“DATA”);         out.println(MsgArea.getSelectedText());         out.println(“.”);         out.println(“QUIT”);      }      catch(Exception e) { System.out.println(“Error ” + e); }   } // sendMsg} //Mailer 

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as