home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / EMail.java < prev    next >
Text File  |  1998-02-26  |  14KB  |  390 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(0);
  139.         }
  140.         catch (IllegalArgumentException e) { }
  141.         keyPressManagerPanel1.add(computers);
  142.         computers.setBounds(529,3,69,21);
  143.         computers.setForeground(new Color(0));
  144.         computers.setBackground(new Color(16777215));
  145.         country = new java.awt.Choice();
  146.         country.addItem("USA");
  147.         country.addItem("Mexico");
  148.         try {
  149.             country.select(0);
  150.         }
  151.         catch (IllegalArgumentException e) { }
  152.         keyPressManagerPanel1.add(country);
  153.         country.setBounds(169,36,102,21);
  154.         country.setForeground(new Color(0));
  155.         country.setBackground(new Color(16777215));
  156.         heardAbout = new java.awt.Choice();
  157.         heardAbout.addItem("Advert");
  158.         heardAbout.addItem("Commercial");
  159.         try {
  160.             heardAbout.select(0);
  161.         }
  162.         catch (IllegalArgumentException e) { }
  163.         keyPressManagerPanel1.add(heardAbout);
  164.         heardAbout.setBounds(427,33,111,21);
  165.         heardAbout.setForeground(new Color(0));
  166.         heardAbout.setBackground(new Color(16777215));
  167.         heardAboutLabel = new java.awt.Label("How you heard about us:");
  168.         heardAboutLabel.setBounds(280,34,142,23);
  169.         keyPressManagerPanel1.add(heardAboutLabel);
  170.         coutryLabel = new java.awt.Label("Country:");
  171.         coutryLabel.setBounds(101,35,50,23);
  172.         keyPressManagerPanel1.add(coutryLabel);
  173.         comments = new java.awt.TextArea();
  174.         comments.setBounds(82,66,513,48);
  175.         keyPressManagerPanel1.add(comments);
  176.         Send = new java.awt.Button();
  177.         Send.setLabel("Send");
  178.         Send.setBounds(82,121,108,24);
  179.         keyPressManagerPanel1.add(Send);
  180.         newButton = new java.awt.Button();
  181.         newButton.setLabel("Reset");
  182.         newButton.setBounds(487,121,108,24);
  183.         keyPressManagerPanel1.add(newButton);
  184.         lastNameLabel = new java.awt.Label("Last Name:");
  185.         lastNameLabel.setBounds(181,3,65,23);
  186.         keyPressManagerPanel1.add(lastNameLabel);
  187.         commentsLabel = new java.awt.Label("Comments:");
  188.         commentsLabel.setBounds(10,84,67,23);
  189.         keyPressManagerPanel1.add(commentsLabel);
  190.         messageLabel = new java.awt.Label("First Name:");
  191.         messageLabel.setBounds(3,4,66,23);
  192.         keyPressManagerPanel1.add(messageLabel);
  193.         computersLabel = new java.awt.Label("How many PC's in your office:");
  194.         computersLabel.setBounds(363,2,162,23);
  195.         keyPressManagerPanel1.add(computersLabel);
  196.         //}}
  197.         try {
  198.             labelHTMLLink1.setText("Back to main");
  199.         }
  200.         catch(java.beans.PropertyVetoException e) { }
  201.         try {
  202.             labelHTMLLink1.setURL(new java.net.URL(URLforward.getText()));
  203.         } catch (java.net.MalformedURLException error) {}
  204.         catch(java.beans.PropertyVetoException e) { }
  205.         
  206.     
  207.         //{{REGISTER_LISTENERS
  208.         Action lAction = new Action();
  209.         Send.addActionListener(lAction);
  210.         newButton.addActionListener(lAction);
  211.         //}}
  212.     }
  213.  //-------------------------------------------------------------
  214.  // This file has been migrated from the 1.0 to 1.1 event model.
  215.  // This method is not used with the new 1.1 event model. You can 
  216.  // move any code you need to keep, then remove this method.
  217.  //-------------------------------------------------------------
  218.  // 
  219.  //     public boolean handleEvent(Event event) {
  220.  //         if (event.target == Send && event.id == Event.ACTION_EVENT)
  221.  //         {
  222.  //             sendMessage();
  223.  //         }
  224.  //         if (event.target == newButton && event.id == Event.ACTION_EVENT)
  225.  //         {
  226.  //             prepForNewMessage();
  227.  //         }
  228.  //         return super.handleEvent(event);
  229.  //     }
  230.  //-------------------------------------------------------------
  231.  
  232.     //Resets all TextFields and textArea in preparation for a new message.
  233.     void prepForNewMessage(java.awt.event.ActionEvent event)
  234.     {
  235.         comments.setText("");
  236.         serverResponse.setText("");
  237.         firstName.setText("");
  238.         lastName.setText("");
  239.  
  240.     }
  241.  
  242.     //This is the method responsible for establishing a communications
  243.     //path to the mail server and for sending SMTP commands.
  244.     //  Step 1:  Create a new socket object using the name of the mail
  245.     //             server and the port number.
  246.     //  Step 2:  Create a printstream object based on the new socket
  247.     //  Step 3:  Send the mail server the appropriate SMTP commands
  248.     //             using the printstream.
  249.     //  Step 4:  This sample also captures and displays status messages
  250.     //             return from the mail server.  See getReply() method
  251.     //             below.
  252.     //  Step 5:  Release the connection to the server.
  253.     //  Step 6:  Close the printstream and the socket.
  254.     void sendMessage(java.awt.event.ActionEvent event)
  255.     {
  256.         //Insert sending host name below...
  257.         String sHostName = new String(mailServer.getText());
  258.  
  259.         //Insert sendmail port number on sending host machin below...
  260.         //(Note:  Port will typically be port #25.)
  261.         int portNum = 25;
  262.  
  263.         try
  264.         {
  265.             //Step 1
  266.             Socket outgoing = new Socket(sHostName, portNum, true);
  267.  
  268.             //Step 2
  269.             PrintStream ps = new PrintStream(outgoing.getOutputStream());
  270.  
  271.             //Steps 3 & 4
  272.             ps.println("HELO " + sHostName);
  273.             serverResponse.appendText(getReply(outgoing) + "\n");
  274.  
  275.             ps.println("MAIL FROM: " + senderName.getText());
  276.             serverResponse.appendText(getReply(outgoing) + "\n");
  277.  
  278.             ps.println("RCPT TO: " + recipientName.getText());
  279.             serverResponse.appendText(getReply(outgoing) + "\n");
  280.  
  281.             ps.println("DATA");
  282.             serverResponse.appendText(getReply(outgoing) + "\n");
  283.  
  284.             ps.println("Subject: "  + subjectLine.getText() + "\n" 
  285.             + firstName.getText() + "\n"
  286.             + lastName.getText() + "\n"
  287.             + country.getSelectedItem() + "\n" 
  288.             + computers.getSelectedItem() + "\n" 
  289.             + heardAbout.getSelectedItem() + "\n" 
  290.             + comments.getText() + "\n" 
  291.             + "\n" + "." + "\n");
  292.  
  293.             serverResponse.appendText(getReply(outgoing) + "\n");
  294.  
  295.             //Step 5
  296.             ps.println("QUIT");
  297.             serverResponse.appendText(getReply(outgoing) + "\n");
  298.  
  299.             //Step 6
  300.             ps.close();
  301.             outgoing.close();
  302.         }
  303.         catch (IOException Ex)
  304.         {
  305.             System.out.println(Ex.getMessage());
  306.         }
  307.         //hides the form once sent
  308.         //{{CONNECTION
  309.         // Hide the KeyPressManagerPanel
  310.         //Customize message after send of mail
  311.         //shows the HTML link back to where you want them to go
  312.         keyPressManagerPanel1.hide();
  313.         Title.setText(successArea.getText());
  314.         labelHTMLLink1.show();
  315.         //}}
  316.  
  317.  
  318.  
  319.     }
  320.  
  321.     //This method will capture status message returned by the mail server
  322.     //and display in the the Server Response TextArea on the Applet.
  323.     //Step 1:  Create an InputStream for the socket object created above that gets passed
  324.     //          into this method.
  325.     //Step 2:  Read a line of the InputStream and return the line as a string for display.
  326.     String getReply(Socket incoming)
  327.     {
  328.         try
  329.         {
  330.             //Step 1
  331.             DataInputStream myDIS = new java.io.DataInputStream(incoming.getInputStream());
  332.  
  333.             //Step 2            
  334.             return myDIS.readLine();
  335.         }
  336.         catch (java.io.IOException Ex)
  337.         {
  338.             return Ex.getMessage();
  339.         }
  340.     }
  341.  
  342.     //{{DECLARE_CONTROLS
  343.     java.awt.Label Title;
  344.     symantec.itools.awt.WrappingLabel Info;
  345.     symantec.itools.awt.WrappingLabel URLforwardLabel;
  346.     java.awt.TextArea serverResponse;
  347.     java.awt.TextArea successArea;
  348.     java.awt.TextField mailServer;
  349.     java.awt.TextField subjectLine;
  350.     java.awt.TextField recipientName;
  351.     java.awt.TextField senderName;
  352.     java.awt.TextField URLforward;
  353.     symantec.itools.awt.WrappingLabel serverResponseLabel;
  354.     java.awt.Label senderLabel;
  355.     java.awt.Label recipientLabel;
  356.     java.awt.Label subjectLabel;
  357.     java.awt.Label mailServerLabel;
  358.     symantec.itools.awt.WrappingLabel successAreaLabel;
  359.     symantec.itools.awt.LabelHTMLLink labelHTMLLink1;
  360.     symantec.itools.awt.KeyPressManagerPanel keyPressManagerPanel1;
  361.     java.awt.TextField firstName;
  362.     java.awt.TextField lastName;
  363.     java.awt.Choice computers;
  364.     java.awt.Choice country;
  365.     java.awt.Choice heardAbout;
  366.     java.awt.Label heardAboutLabel;
  367.     java.awt.Label coutryLabel;
  368.     java.awt.TextArea comments;
  369.     java.awt.Button Send;
  370.     java.awt.Button newButton;
  371.     java.awt.Label lastNameLabel;
  372.     java.awt.Label commentsLabel;
  373.     java.awt.Label messageLabel;
  374.     java.awt.Label computersLabel;
  375.     //}}
  376.  
  377.  
  378.     class Action implements java.awt.event.ActionListener
  379.     {
  380.         public void actionPerformed(java.awt.event.ActionEvent event)
  381.         {
  382.             Object object = event.getSource();
  383.             if (object == Send)
  384.                 sendMessage(event);
  385.             else if (object == newButton)
  386.                 prepForNewMessage(event);
  387.         }
  388.     }
  389. }
  390.