email applet

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
java_kevinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fontaineCommented:
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
fontaineCommented:
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
fontaineCommented:
This page has a mailer, source code included:

http://www.ozemail.com.au/~geoffk/anon/anon.html
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

java_kevinAuthor Commented:
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
fontaineCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fontaineCommented:
[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
fontaineCommented:
[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
fontaineCommented:
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
java_kevinAuthor Commented:
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
fontaineCommented:
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
java_kevinAuthor Commented:
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
java_kevinAuthor Commented:
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
fontaineCommented:
I was just writing some sample code for you! But I am too stupid!!! Good luck!
0
java_kevinAuthor Commented:
So what do i write in my applet?

When do i call the methods?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.