luliu

Application Code for the Send Mail App

Apr 28th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package sendMailApp;
  2.     /*
  3.      *  Application Code for the Send Mail App
  4.      *  
  5.      *  Lu Liu
  6.      *  CSCI-211 Intermediate Java Programming
  7.      *  4/20/2015
  8.      */
  9. import java.io.DataInputStream;
  10. import java.io.DataOutputStream;
  11. import java.io.PrintWriter;
  12. import java.net.Socket;
  13. import java.util.Scanner;
  14.  
  15. import javafx.application.Application;
  16. import javafx.geometry.Insets;
  17. import javafx.geometry.Pos;
  18. import javafx.scene.Scene;
  19. import javafx.scene.control.Button;
  20. import javafx.scene.control.Label;
  21. import javafx.scene.control.TextArea;
  22. import javafx.scene.control.TextField;
  23. import javafx.scene.layout.BorderPane;
  24. import javafx.scene.layout.FlowPane;
  25. import javafx.stage.Stage;
  26.  
  27.     public class SendMailApp extends Application {
  28.         private TextField txtTo;
  29.         private TextField txtFrom;
  30.         private TextField txtSubject;
  31.         private TextArea  txtBody;
  32.         private TextField txtStatus;
  33.         private Button btnSend;
  34.  
  35.         @Override
  36.         // Override the start method in the Application class
  37.         public void start(Stage primaryStage) {
  38.             txtTo = new TextField();
  39.             txtTo.setPrefColumnCount(25);
  40.             txtFrom = new TextField();
  41.             txtFrom.setPrefColumnCount(25);
  42.             txtSubject = new TextField();
  43.             txtSubject.setPrefColumnCount(25);
  44.             txtBody = new TextArea();
  45.             txtBody.setPrefColumnCount(28);
  46.             txtBody.setPrefRowCount(10);
  47.             btnSend = new Button("Send Email");
  48.             txtStatus = new TextField();
  49.             txtStatus.setPrefColumnCount(25);
  50.             txtStatus.setEditable(false);
  51.            
  52.             Label lblTo = new Label("Mail To:");
  53.             lblTo.setPrefWidth(90);
  54.             Label lblFrom = new Label("Mail From:");
  55.             lblFrom.setPrefWidth(90);
  56.             Label lblSubject = new Label("Subject:");
  57.             lblSubject.setPrefWidth(90);
  58.            
  59.             FlowPane tpane = new FlowPane(5, 5);
  60.             tpane.setAlignment(Pos.BASELINE_LEFT);
  61.             tpane.setPadding(new Insets(10, 5, 5, 5));
  62.             tpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
  63.             tpane.getChildren().addAll(lblTo, txtTo, lblFrom, txtFrom, lblSubject, txtSubject);
  64.            
  65.             FlowPane cpane = new FlowPane(5, 5);
  66.             cpane.setAlignment(Pos.CENTER);
  67.             cpane.setPadding(new Insets(5, 5, 5, 5));
  68.             cpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
  69.             cpane.getChildren().addAll(new Label("Enter your message below:"), txtBody, btnSend);
  70.  
  71.             Label lblStatus = new Label("Status:");
  72.             lblStatus.setPrefWidth(90);
  73.             FlowPane bpane = new FlowPane(5, 5);
  74.             bpane.setAlignment(Pos.BASELINE_LEFT);
  75.             bpane.setPadding(new Insets(5, 5, 10, 5));
  76.             bpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
  77.             bpane.getChildren().addAll(lblStatus, txtStatus);
  78.  
  79.             BorderPane bp = new BorderPane();
  80.             bp.setTop(tpane);
  81.             bp.setCenter(cpane);
  82.             bp.setBottom(bpane);
  83.            
  84.             btnSend.setOnAction(e -> {
  85.                 txtStatus.setText("Message Sent Successfully");
  86.                 runClient();
  87.             });
  88.  
  89.             Scene scene = new Scene(bp, 525, 520);
  90.             primaryStage.setTitle("Send Mail Application");
  91.             primaryStage.setScene(scene);
  92.             primaryStage.show();
  93.             primaryStage.setResizable(false);
  94.         }
  95.  
  96.         /**
  97.          * The main method is only needed for the IDE with limited JavaFX support.
  98.          * Not needed for running from the command line.
  99.          */
  100.         public static void main(String[] args) {
  101.             launch(args);
  102.         }
  103.        
  104.         public void runClient(){       
  105.             Socket client = null;  
  106.             PrintWriter output = null;
  107.             Scanner input = null;
  108. //          DataOutputStream output = null;
  109. //          DataInputStream input = null;
  110.            
  111.             try {  
  112.                 // Create a socket to connect to the server
  113.                 client = new Socket("cs.stcc.edu", 25);
  114.                 input.nextLine();
  115.              
  116.                 //HELO
  117.                 output = new PrintWriter("HELO cs.stcc.edu");
  118.                 System.out.println(input.nextLine());
  119.                 output.flush();
  120.  
  121.                 //Mail From
  122.                 output.println(txtTo.getText());
  123.                 System.out.println(input.nextLine());
  124.                 output.flush();
  125.  
  126.                 //Mail To
  127.                 output.println(txtFrom.getText());
  128.                 System.out.println(input.nextLine());
  129.                 output.flush();        
  130.  
  131.                 //Message Body
  132.                 output.println("DATA");
Add Comment
Please, Sign In to add comment