home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / VCSAMPL.BIN / EMail.java < prev    next >
Text File  |  1997-09-09  |  14KB  |  384 lines

  1. /*
  2.     A basic extension of the java.applet.Applet class
  3.  */
  4. //package symantec.itools.awt;
  5. import java.awt.*;
  6. import java.applet.*;
  7. import java.net.*;
  8. import java.io.*;
  9.  
  10. import symantec.itools.awt.LabelHTMLLink;
  11. import symantec.itools.awt.WrappingLabel;
  12. import symantec.itools.awt.KeyPressManagerPanel;
  13. public class EMail extends Applet {
  14.  
  15.  
  16.     public void init() {
  17.         super.init();
  18.  
  19.         //{{INIT_CONTROLS
  20.         setLayout(null);
  21.         setSize(666,460);
  22.         setFont(new Font("Dialog", Font.PLAIN, 12));
  23.         setForeground(new Color(0));
  24.         setBackground(new Color(12632256));
  25.         Title = new java.awt.Label("E-Mail Response Form",Label.CENTER);
  26.         Title.setBounds(93,3,411,28);
  27.         Title.setFont(new Font("Dialog", Font.BOLD, 24));
  28.         add(Title);
  29.         Info = new symantec.itools.awt.WrappingLabel();
  30.         try {
  31.             Info.setText("The area below is for configuration, it is not visible to the users at run time. Click the text field and set each text property in the Property List according to the instructions to the left of the field.");
  32.         }
  33.         catch(java.beans.PropertyVetoException e) { }
  34.         try {
  35.             Info.setAlignStyle(symantec.itools.awt.WrappingLabel.ALIGN_CENTERED);
  36.         }
  37.         catch(java.beans.PropertyVetoException e) { }
  38.         Info.setVisible(false);
  39.         Info.setBounds(33,179,552,35);
  40.         add(Info);
  41.         URLforwardLabel = new symantec.itools.awt.WrappingLabel();
  42.         try {
  43.             URLforwardLabel.setText("Set the forwarding URL of the page you want to link to after a successful mail send");
  44.         }
  45.         catch(java.beans.PropertyVetoException e) { }
  46.         URLforwardLabel.setVisible(false);
  47.         URLforwardLabel.setBounds(33,330,326,31);
  48.         add(URLforwardLabel);
  49.         serverResponse = new java.awt.TextArea();
  50.         serverResponse.setVisible(false);
  51.         serverResponse.setBounds(383,410,237,40);
  52.         add(serverResponse);
  53.         successArea = new java.awt.TextArea();
  54.         successArea.setText("Thanks for registering!");
  55.         successArea.setVisible(false);
  56.         successArea.setBounds(383,365,237,41);
  57.         add(successArea);
  58.         mailServer = new java.awt.TextField();
  59.         mailServer.setText("mail.yourmailserver.com");
  60.         mailServer.setVisible(false);
  61.         mailServer.setBounds(383,302,237,24);
  62.         mailServer.setBackground(new Color(16777215));
  63.         add(mailServer);
  64.         subjectLine = new java.awt.TextField();
  65.         subjectLine.setText("Registration Email Response");
  66.         subjectLine.setVisible(false);
  67.         subjectLine.setBounds(383,272,237,24);
  68.         add(subjectLine);
  69.         recipientName = new java.awt.TextField();
  70.         recipientName.setText("youremail@yourmail.com");
  71.         recipientName.setVisible(false);
  72.         recipientName.setBounds(383,244,237,24);
  73.         add(recipientName);
  74.         senderName = new java.awt.TextField();
  75.         senderName.setText("youremail@yourmail.com");
  76.         senderName.setVisible(false);
  77.         senderName.setBounds(383,216,237,24);
  78.         add(senderName);
  79.         URLforward = new java.awt.TextField();
  80.         URLforward.setText("http://www.yourmailweb.com");
  81.         URLforward.setVisible(false);
  82.         URLforward.setBounds(383,332,237,24);
  83.         add(URLforward);
  84.         serverResponseLabel = new symantec.itools.awt.WrappingLabel();
  85.         try {
  86.             serverResponseLabel.setText("This window is for server response, change its Visible property to view the server execution commands for debugging.");
  87.         }
  88.         catch(java.beans.PropertyVetoException e) { }
  89.         serverResponseLabel.setVisible(false);
  90.         serverResponseLabel.setBounds(33,410,301,49);
  91.         add(serverResponseLabel);
  92.         senderLabel = new java.awt.Label("Set the email address for the email to be marked as from");
  93.         senderLabel.setVisible(false);
  94.         senderLabel.setBounds(33,215,352,23);
  95.         add(senderLabel);
  96.         recipientLabel = new java.awt.Label("Set the email address for the response form to send to");
  97.         recipientLabel.setVisible(false);
  98.         recipientLabel.setBounds(33,242,318,23);
  99.         add(recipientLabel);
  100.         subjectLabel = new java.awt.Label("Set the subject that the Email will have");
  101.         subjectLabel.setVisible(false);
  102.         subjectLabel.setBounds(33,270,317,23);
  103.         add(subjectLabel);
  104.         mailServerLabel = new java.awt.Label("Set the mail server you will be using");
  105.         mailServerLabel.setVisible(false);
  106.         mailServerLabel.setBounds(33,300,317,23);
  107.         add(mailServerLabel);
  108.         successAreaLabel = new symantec.itools.awt.WrappingLabel();
  109.         try {
  110.             successAreaLabel.setText("Type in any text you wish displayed to the user after sending the mail");
  111.         }
  112.         catch(java.beans.PropertyVetoException e) { }
  113.         successAreaLabel.setVisible(false);
  114.         successAreaLabel.setBounds(33,370,301,34);
  115.         add(successAreaLabel);
  116.         labelHTMLLink1 = new symantec.itools.awt.LabelHTMLLink();
  117.         try {
  118.             labelHTMLLink1.setText("Back to the site");
  119.         }
  120.         catch(java.beans.PropertyVetoException e) { }
  121.         labelHTMLLink1.setVisible(false);
  122.         labelHTMLLink1.setBounds(504,7,122,22);
  123.         add(labelHTMLLink1);
  124.         keyPressManagerPanel1 = new symantec.itools.awt.KeyPressManagerPanel();
  125.         keyPressManagerPanel1.setLayout(null);
  126.         keyPressManagerPanel1.setBounds(15,31,618,145);
  127.         add(keyPressManagerPanel1);
  128.         firstName = new java.awt.TextField();
  129.         firstName.setBounds(73,5,104,21);
  130.         keyPressManagerPanel1.add(firstName);
  131.         lastName = new java.awt.TextField();
  132.         lastName.setBounds(251,2,108,21);
  133.         keyPressManagerPanel1.add(lastName);
  134.         computers = new java.awt.Choice();
  135.         computers.addItem("1");
  136.         computers.addItem("2");
  137.         try {
  138.             computers.select(-1);
  139.         } catch (IllegalArgumentException e) { }
  140.         keyPressManagerPanel1.add(computers);
  141.         computers.setBounds(529,3,69,21);
  142.         computers.setForeground(new Color(0));
  143.         computers.setBackground(new Color(16777215));
  144.         country = new java.awt.Choice();
  145.         country.addItem("USA");
  146.         country.addItem("Mexico");
  147.         keyPressManagerPanel1.add(country);
  148.         country.setBounds(169,36,102,21);
  149.         country.setForeground(new Color(0));
  150.         country.setBackground(new Color(16777215));
  151.         heardAbout = new java.awt.Choice();
  152.         heardAbout.addItem("Advert");
  153.         heardAbout.addItem("Commercial");
  154.         try {
  155.             heardAbout.select(-1);
  156.         } catch (IllegalArgumentException e) { }
  157.         keyPressManagerPanel1.add(heardAbout);
  158.         heardAbout.setBounds(427,33,111,21);
  159.         heardAbout.setForeground(new Color(0));
  160.         heardAbout.setBackground(new Color(16777215));
  161.         heardAboutLabel = new java.awt.Label("How you heard about us:");
  162.         heardAboutLabel.setBounds(280,34,142,23);
  163.         keyPressManagerPanel1.add(heardAboutLabel);
  164.         coutryLabel = new java.awt.Label("Country:");
  165.         coutryLabel.setBounds(101,35,50,23);
  166.         keyPressManagerPanel1.add(coutryLabel);
  167.         comments = new java.awt.TextArea();
  168.         comments.setBounds(82,66,513,48);
  169.         keyPressManagerPanel1.add(comments);
  170.         Send = new java.awt.Button();
  171.         Send.setLabel("Send");
  172.         Send.setBounds(82,121,108,24);
  173.         keyPressManagerPanel1.add(Send);
  174.         newButton = new java.awt.Button();
  175.         newButton.setLabel("Reset");
  176.         newButton.setBounds(487,121,108,24);
  177.         keyPressManagerPanel1.add(newButton);
  178.         lastNameLabel = new java.awt.Label("Last Name:");
  179.         lastNameLabel.setBounds(181,3,65,23);
  180.         keyPressManagerPanel1.add(lastNameLabel);
  181.         commentsLabel = new java.awt.Label("Comments:");
  182.         commentsLabel.setBounds(10,84,67,23);
  183.         keyPressManagerPanel1.add(commentsLabel);
  184.         messageLabel = new java.awt.Label("First Name:");
  185.         messageLabel.setBounds(3,4,66,23);
  186.         keyPressManagerPanel1.add(messageLabel);
  187.         computersLabel = new java.awt.Label("How many PC's in your office:");
  188.         computersLabel.setBounds(363,2,162,23);
  189.         keyPressManagerPanel1.add(computersLabel);
  190.         //}}
  191.         try {
  192.             labelHTMLLink1.setText("Back to main");
  193.         }
  194.         catch(java.beans.PropertyVetoException e) { }
  195.         try {
  196.             labelHTMLLink1.setURL(new java.net.URL(URLforward.getText()));
  197.         } catch (java.net.MalformedURLException error) {}
  198.         catch(java.beans.PropertyVetoException e) { }
  199.         
  200.     
  201.         //{{REGISTER_LISTENERS
  202.         Action lAction = new Action();
  203.         Send.addActionListener(lAction);
  204.         newButton.addActionListener(lAction);
  205.         //}}
  206.     }
  207.  //-------------------------------------------------------------
  208.  // This file has been migrated from the 1.0 to 1.1 event model.
  209.  // This method is not used with the new 1.1 event model. You can 
  210.  // move any code you need to keep, then remove this method.
  211.  //-------------------------------------------------------------
  212.  // 
  213.  //     public boolean handleEvent(Event event) {
  214.  //         if (event.target == Send && event.id == Event.ACTION_EVENT)
  215.  //         {
  216.  //             sendMessage();
  217.  //         }
  218.  //         if (event.target == newButton && event.id == Event.ACTION_EVENT)
  219.  //         {
  220.  //             prepForNewMessage();
  221.  //         }
  222.  //         return super.handleEvent(event);
  223.  //     }
  224.  //-------------------------------------------------------------
  225.  
  226.     //Resets all TextFields and textArea in preparation for a new message.
  227.     void prepForNewMessage(java.awt.event.ActionEvent event)
  228.     {
  229.         comments.setText("");
  230.         serverResponse.setText("");
  231.         firstName.setText("");
  232.         lastName.setText("");
  233.  
  234.     }
  235.  
  236.     //This is the method responsible for establishing a communications
  237.     //path to the mail server and for sending SMTP commands.
  238.     //  Step 1:  Create a new socket object using the name of the mail
  239.     //             server and the port number.
  240.     //  Step 2:  Create a printstream object based on the new socket
  241.     //  Step 3:  Send the mail server the appropriate SMTP commands
  242.     //             using the printstream.
  243.     //  Step 4:  This sample also captures and displays status messages
  244.     //             return from the mail server.  See getReply() method
  245.     //             below.
  246.     //  Step 5:  Release the connection to the server.
  247.     //  Step 6:  Close the printstream and the socket.
  248.     void sendMessage(java.awt.event.ActionEvent event)
  249.     {
  250.         //Insert sending host name below...
  251.         String sHostName = new String(mailServer.getText());
  252.  
  253.         //Insert sendmail port number on sending host machin below...
  254.         //(Note:  Port will typically be port #25.)
  255.         int portNum = 25;
  256.  
  257.         try
  258.         {
  259.             //Step 1
  260.             Socket outgoing = new Socket(sHostName, portNum, true);
  261.  
  262.             //Step 2
  263.             PrintStream ps = new PrintStream(outgoing.getOutputStream());
  264.  
  265.             //Steps 3 & 4
  266.             ps.println("HELO " + sHostName);
  267.             serverResponse.appendText(getReply(outgoing) + "\n");
  268.  
  269.             ps.println("MAIL FROM: " + senderName.getText());
  270.             serverResponse.appendText(getReply(outgoing) + "\n");
  271.  
  272.             ps.println("RCPT TO: " + recipientName.getText());
  273.             serverResponse.appendText(getReply(outgoing) + "\n");
  274.  
  275.             ps.println("DATA");
  276.             serverResponse.appendText(getReply(outgoing) + "\n");
  277.  
  278.             ps.println("Subject: "  + subjectLine.getText() + "\n" 
  279.             + firstName.getText() + "\n"
  280.             + lastName.getText() + "\n"
  281.             + country.getSelectedItem() + "\n" 
  282.             + computers.getSelectedItem() + "\n" 
  283.             + heardAbout.getSelectedItem() + "\n" 
  284.             + comments.getText() + "\n" 
  285.             + "\n" + "." + "\n");
  286.  
  287.             serverResponse.appendText(getReply(outgoing) + "\n");
  288.  
  289.             //Step 5
  290.             ps.println("QUIT");
  291.             serverResponse.appendText(getReply(outgoing) + "\n");
  292.  
  293.             //Step 6
  294.             ps.close();
  295.             outgoing.close();
  296.         }
  297.         catch (IOException Ex)
  298.         {
  299.             System.out.println(Ex.getMessage());
  300.         }
  301.         //hides the form once sent
  302.         //{{CONNECTION
  303.         // Hide the KeyPressManagerPanel
  304.         //Customize message after send of mail
  305.         //shows the HTML link back to where you want them to go
  306.         keyPressManagerPanel1.hide();
  307.         Title.setText(successArea.getText());
  308.         labelHTMLLink1.show();
  309.         //}}
  310.  
  311.  
  312.  
  313.     }
  314.  
  315.     //This method will capture status message returned by the mail server
  316.     //and display in the the Server Response TextArea on the Applet.
  317.     //Step 1:  Create an InputStream for the socket object created above that gets passed
  318.     //          into this method.
  319.     //Step 2:  Read a line of the InputStream and return the line as a string for display.
  320.     String getReply(Socket incoming)
  321.     {
  322.         try
  323.         {
  324.             //Step 1
  325.             DataInputStream myDIS = new java.io.DataInputStream(incoming.getInputStream());
  326.  
  327.             //Step 2            
  328.             return myDIS.readLine();
  329.         }
  330.         catch (java.io.IOException Ex)
  331.         {
  332.             return Ex.getMessage();
  333.         }
  334.     }
  335.  
  336.     //{{DECLARE_CONTROLS
  337.     java.awt.Label Title;
  338.     symantec.itools.awt.WrappingLabel Info;
  339.     symantec.itools.awt.WrappingLabel URLforwardLabel;
  340.     java.awt.TextArea serverResponse;
  341.     java.awt.TextArea successArea;
  342.     java.awt.TextField mailServer;
  343.     java.awt.TextField subjectLine;
  344.     java.awt.TextField recipientName;
  345.     java.awt.TextField senderName;
  346.     java.awt.TextField URLforward;
  347.     symantec.itools.awt.WrappingLabel serverResponseLabel;
  348.     java.awt.Label senderLabel;
  349.     java.awt.Label recipientLabel;
  350.     java.awt.Label subjectLabel;
  351.     java.awt.Label mailServerLabel;
  352.     symantec.itools.awt.WrappingLabel successAreaLabel;
  353.     symantec.itools.awt.LabelHTMLLink labelHTMLLink1;
  354.     symantec.itools.awt.KeyPressManagerPanel keyPressManagerPanel1;
  355.     java.awt.TextField firstName;
  356.     java.awt.TextField lastName;
  357.     java.awt.Choice computers;
  358.     java.awt.Choice country;
  359.     java.awt.Choice heardAbout;
  360.     java.awt.Label heardAboutLabel;
  361.     java.awt.Label coutryLabel;
  362.     java.awt.TextArea comments;
  363.     java.awt.Button Send;
  364.     java.awt.Button newButton;
  365.     java.awt.Label lastNameLabel;
  366.     java.awt.Label commentsLabel;
  367.     java.awt.Label messageLabel;
  368.     java.awt.Label computersLabel;
  369.     //}}
  370.  
  371.  
  372.     class Action implements java.awt.event.ActionListener
  373.     {
  374.         public void actionPerformed(java.awt.event.ActionEvent event)
  375.         {
  376.             Object object = event.getSource();
  377.             if (object == Send)
  378.                 sendMessage(event);
  379.             else if (object == newButton)
  380.                 prepForNewMessage(event);
  381.         }
  382.     }
  383. }
  384.