Solved

email applet

Posted on 1998-05-07
14
422 Views
Last Modified: 2013-11-24
I saw this type of applet can be done form a java book. Tried to go to the url. But not there.

So can someone tel mi the code to write an applet which:

allows someone to email mi through the webpg without having access to email in their browser
0
Comment
Question by:java_kevin
  • 9
  • 5
14 Comments
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
Use the following class. Due to security restriction, an applet can only connect to the machine from which it has been loaded. Hence, a mail server has to be installed on the same machine as the one serving the applet.

//***************************************************************************
// Name: email.java
// Date: 03-Sep-96
// Prog: Randy Layman
//
// Desc: This is a rough implementation of a class which is able to
//       collect email message details and then connect to an email
//       server on port 25 (the mail port) to send it.  This class can
//       be used by an application, or an applet if the server hosting
//       the applet has a mail server available.
//
// Note: Error checking when attempting to communicate with the mail
//       server is minimal.  This will need to be beefed up in order to
//       produce an applet or application robust enough for commercial
//       use.
//
// Copyright 1996, Custom Innovative Solutions
//
// If you are earning an income while viewing our material you should
// probably be paying fees to comply with our copyright requirements.
// However, if you are a student you are likely exempt.
//
// Please visit our site to see our licensing policy and how to report
// usage:
//
//    http://www.cisc.com/copyright.html
//    http://www.cisc.com/reporting.html
//
// If you find our material helpful, please link to our home page.
//***************************************************************************

//**********************************************
// We use networking, I/O, and several utilities
//**********************************************
import java.net.*;
import java.io.*;
import java.util.Date;
import java.util.StringTokenizer;

//*********************************************************
// Allow message construction and then delivery via port 25
//*********************************************************
public class email
{
   //*******************
   // Instance variables
   //*******************
   private String host = null;
   private String to = null;
   private String from = null;
   private String subject = null;
   private String cc = null;
   private String bcc = null;
   private String body = null;
   private Socket server = null;
   private Date sent = null;
   private DataInputStream input = null;
   private OutputStream out = null;

   //*******************************************************************
   // Constructor for email class.  We collect the host that will be
   // used to send the email, and the userid the message should be from.
   // If not '@' is found in the from address the machine name from the
   // host value will be appended to it elsewhere.
   //*******************************************************************
   public email(String host, String from)
   {
      this.host = host;
      this.from = from;
   }

   //********************************
   // Who is the email to be sent to?
   //********************************
   public void setTo(String to)
   {
      this.to = to;
   }

   //*********************
   // What is the subject?
   //*********************
   public void setSubject(String subject)
   {
      this.subject = subject;
   }

   //***********************************
   // Who is to be CC'd (Carbon Copied)?
   //***********************************
   public void setCc(String cc)
   {
      this.cc = cc;
   }

   //******************************************
   // Who is to be BCC'd (Blind Carbon Copied)?
   //******************************************
   public void setBcc(String bcc)
   {
      this.bcc = bcc;
   }

   //***********************************
   // What message (body) is to be sent?
   //***********************************
   public void setBody(String body)
   {
      this.body = body;
   }

   //************************************
   // Date sent, whatever the time is now
   //************************************
   public void setDate()
   {
      this.sent = new Date();
   }

   //*******************************
   // Date sent, as a specified time
   //*******************************
   public void setDate(Date sent)
   {
      this.sent = sent;
   }

   //************************************************************************
   // After setting all necessary values above the programmer simply
   // calls the send method.  The send method attempts to connect, parse
   // the values provided above, and talk to the mail server.  At this
   // time error checking is minimal or non-existent.
   //************************************************************************
   public boolean send()
   {
      //---------------------------------------------------------
      // Fail if we cant create a socket connection to the server
      //---------------------------------------------------------
      if ( !createSocket() )
         return false;

      //--------------------------------------
      // Send to each recipient in the TO list
      //--------------------------------------
      StringTokenizer st = new StringTokenizer(to, ",");
      while(st.countTokens() > 0)
         createMessage(st.nextToken());

      //--------------------------------------
      // Send to each recipient in the CC list
      //--------------------------------------
      if (cc != null)
      {
         StringTokenizer st2 = new StringTokenizer(cc, ",");
         while(st2.countTokens() > 0)
            createMessage(st2.nextToken());
      }

      //---------------------------------------
      // Send to each recipient in the BCC list
      //---------------------------------------
      if (bcc != null)
      {
         StringTokenizer st3 = new StringTokenizer(bcc, ",");
         while (st3.countTokens() > 0)
            createMessage(st3.nextToken());
      }

      //------------------------------------------------------
      // Close the connection to the server and return success
      //------------------------------------------------------
      closeSocket();
      return true;
   }

   //****************************************************************
   // Private utility method attaching to the mail server and sending
   // the HELO message.  We should check that communication is
   // allowed right now, and ensure that SMTP is the protocol!!!
   //****************************************************************
   private boolean createSocket()
   {
      try
      {
         //-------------------------------------------------
         // Attemp to connect and get read and write streams
         //-------------------------------------------------
         server = new Socket(host, 25);
         input = new DataInputStream(server.getInputStream());
         out = server.getOutputStream();

         sendData("HELO\n");
         return true;
      }
      catch (Exception e)
      {
         //----------------------------------------------------
         // Send errors to standard out for use when developing
         //----------------------------------------------------
         System.out.println("Problem creating the socket");
         System.out.println(e.getMessage());
         return false;
      }
   }

   //*******************************************************************
   // Create a message by putting together a series of strings following
   // the expected protocol.  Each string is passed to the mail server
   // using method sendData().
   //*******************************************************************
   private boolean createMessage(String thisTo)
   {
      try
      {
         //-----------------------------------
         // Start with from and to information
         //-----------------------------------
         sendData("MAIL FROM: "+from+"\n");
         sendData("RCPT TO: "+thisTo+"\n");

         //------------------------------------------------------
         // Construct header of date, subject, from, to and other
         // informational fields
         //------------------------------------------------------
         String header = new String("Data\n");
         if (sent != null)
         {
            header = header + "Date: "+sent.toString()+"\n";
         }
         else
         {
            sent = new Date();
            header = header + "Date: "+sent.toString()+"\n";
         }
         if (from != null)
            header = header+"From:  "+from+"\n";

         header = header+"To: "+to+"\n";
         if (subject != null)
         {
            header = header+"Subject: "+subject+"\n";
         }
         if (cc != null)
         {
            header = header+"Cc: "+cc+"\n";
         }
         header = header+"X-Mailer: JavaMailer [version 1.00]\n";

         //----------------------------------------------------------------
         // Send the header information, then the message body if it exists
         //----------------------------------------------------------------
         sendData(header);
         if (body != null)
            sendData(body);
         sendData("\n.\n");

         //----------------------------------------------
         // Apparently this worked... but we didn't check
         //----------------------------------------------
         return true;
      }
      catch (Exception e)
      {
         //-----------------------------------------
         // Failure, give some debugging information
         //-----------------------------------------
         System.out.println("Problem creating the Message");
         System.out.println(e.getMessage());
         return false;
      }
   }

   //******************************************************
   // Break strings into single characters for transmission
   //******************************************************
   private void sendData(String str)
   {
      for (int i = 0; i < str.length(); i++)
         transmitch(str.charAt(i));
   }

   //***************************************************************
   // Low level routine sending one single character, and converting
   // newlines '\n' into returns '\r\n'.
   //***************************************************************
   private void transmitch(char c)
   {
      if (c == '\n')
         transmitch('\r');

      try
      {
         out.write((int)c);
         out.flush();
      }
      catch (IOException e)
      {
         System.out.println("Problem writing character...");
      }
   }

   //*******************************************
   // Read a line of input from the input stream
   //*******************************************
   private String readLine(DataInputStream i)
   {
      try
      {
         return i.readLine();
      }
      catch (Exception e)
      {
         //-------------------------------------
         // More development time debugging info
         //-------------------------------------
         System.out.println("Can't read the line");
         System.out.println(e.getMessage());
         return null;
      }
   }

   //***********************************************************
   // Close the connection to the host server, first in terms of
   // protocol, and then the actual socket itself...
   //***********************************************************
   private void closeSocket()
   {
      try
      {
         sendData("QUIT\n");
         server.close();
      }
      catch (Exception e)
      {
         System.out.println("Couldn't close the server socket");
      }
   }
}

0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
If you want a practical example of use of the class above, go to my on-line CV page. The applet that is there has been built using it:

http://www.chez.com/bf/cv_en.html

All that the applet does is to read the data that is in the form, build an email object, initialize it, and call its send() method.
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
This page has a mailer, source code included:

http://www.ozemail.com.au/~geoffk/anon/anon.html
0
 

Author Comment

by:java_kevin
Comment Utility
Is there an easier way?

Do you mean that the applet needs to go through the server form which the aplet loaded from and be sent from there?

Can explain the process?
0
 
LVL 5

Accepted Solution

by:
fontaine earned 60 total points
Comment Utility
Yes indeed! That's due to security restrictions. The applet can only connect to the same machine as the one that served the applet. That's the rule with applets! This is why it is needed to have a mail server on the same machine as the one that served the applet. Of course, what you could do is write a script at the server-side that dispatches the message to a mail server on another machine. Anyway, if you need to establish a network connection, and you need one here, you can only connect to the machine that served the applet (or you have to go through the applet signing process, that requires to get a certificate from a Certificate Authority).

[Note: when you need clarification, you can simply leave a comment, instead of rejecting]
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
[Stange! It seems that my answer has not been posted correctly!]

Yes indeed! That's due to security restrictions. The applet can only connect to the same machine as the one that served the applet. That's the rule with applets! This is why it is needed to have a mail server on the same machine as the one that served the applet. Of course, what you could do is write a script at the server-side that dispatches the message to a mail server on another machine. Anyway, if you need to establish a network connection, and you need one here, you can only connect to the machine that served the applet (or you have to go through the applet signing process, that requires to get a certificate from a Certificate Authority).

[Note: when you need clarification, you can simply leave a comment, instead of rejecting]
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
[Stange! It seems that my answer has not been posted correctly! Here is the text.]

Yes indeed! That's due to security restrictions. The applet can only connect to the same machine as the one that served the applet. That's the rule with applets! This is why it is needed to have a mail server on the same machine as the one that served the applet. Of course, what you could do is write a script at the server-side that dispatches the message to a mail server on another machine. Anyway, if you need to establish a network connection, and you need one here, you can only connect to the machine that served the applet (or you have to go through the applet signing process, that requires to get a certificate from a Certificate Authority).

[Note: when you need clarification, you can simply leave a comment, instead of rejecting]
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:fontaine
Comment Utility
I suppose that you want to add the applet on your personal homepage. Geocities probably has a mail server installed on the same machine as the Web server. This is often the case with this kind of company. I have a free account at CHEZ (www.chez.com) and I am using their mail server in conjunction with the "CV mailer" applet that you maybe saw on my Web page. This applet uses the email class I posted the code above.
0
 

Author Comment

by:java_kevin
Comment Utility
Still looking for a better solution....

But when I used the example on the anon.html, they can't get to the server on time and timeout.

And you didn't include the Applet class. You just had the constructor. How am I going to write the applet class, huh?

Esp. those parts host, from, subject...etc, how am I going to pass them? And how should the values look like??

You simply put the constructor class here and expected me to accept it. This ain't any proper answer.

Right, your answer sounds possible. But this hasn't solved my problem a bit.

What am I going to do without the applet class??
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
OK, I understand: you were looking for someone writing the code for you. You are on the wrong site. Using the email class is simply piece of cake. When you have a setSubject(String subject) with as comment  "What is the subject?" it appears trivial to me that  this serves to set the subject. And if the subject is written in a TextField, getText() gives you it. Difficult, indeed! Also, I point you to a full implementation of an email applet. You had just to pump it out!
0
 

Author Comment

by:java_kevin
Comment Utility
Then what do i write in the applet class??

I know. You ranked in the top. But that doesn't mean your answers MUST be accepted the first time you give. I actually prefer something easier to implements and understand.

And all you gave is not exactly what i want. Including not giving me the applet class. But the constructor only.
0
 

Author Comment

by:java_kevin
Comment Utility
Then what do i write in the applet class??

I know. You ranked in the top. But that doesn't mean your answers MUST be accepted the first time you give. It depends on whether that answer suits me and whether it really answered my question in the way i want.

And all you gave is not exactly what i want. Including not giving me the applet class. But the constructor only.

Lastly, I accepted your answer because I want this to end soon and I don't want to hear complaints form you.
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
I was just writing some sample code for you! But I am too stupid!!! Good luck!
0
 

Author Comment

by:java_kevin
Comment Utility
So what do i write in my applet?

When do i call the methods?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now